I was reading about how functions become bound methods when being set as class atrributes. I then observed that this is not the case for functions that are wrapped by functools.partial. What is the explanation for this? Simple example: I kind of expected them both to behave in the same way. Answer The trick that allows functions to become bound
Tag: functools
Are these `.` attribute bindings necessary in the implementation of `functools.partial`?
docs.python.org says that functools.partial is roughly equivalent to: (Note: / is used to denote func as a positional-only argument of partial. See [1].) If I understand correctly, when a variable is referenced within a nested function, such as newfunc, Python first looks for the variable definition within the nested function. If the definition is not found there, Python will next
How do I use python reduce function to fill NaN values
I have a dataframe with missing data in several columns. In some of these columns, say ‘Col_A’ to ‘Col_D’, I’d like to replace them with 0. I tried it this way: but I got the error message <lambda>() takes 1 positional argument but 2 were given. Eventually, I changed my solution to simply but I still wonder what’s wrong with
@lru_cache decorator excessive cache misses
How can you configure lru_cache to key its cache based on actual values received, rather than how the function was called? In other words, only the first call above should be a cache miss, the other two should be cache hits. Answer To do that, you would have to go through the process of binding arguments to formal parameters. The
Does python functools.singledispatch work with Generator type?
I extended the example at https://docs.python.org/3/library/functools.html#functools.singledispatch by adding a registration for generator type while it works with list, it doesn’t seem to work with generator with error like Is it expected that singledispatch does not work with generator? Answer typing.Generator is a type hint, not a type. You need types.GeneratorType. Objects are not considered instances of type hints according to
Can One Replace or Remove a specific key from functools.lru_cache?
I’m using a functools.lru_cache to serve temp file paths given certain input*. However in case the path no longer exists, I would like to remove/replace the single corresponding key. The cache_clear() method would be overkill and cache_info() do not appear to help. Thanks for your help! * The method being cached streams a fileobj from S3 to a local temp
Python functools lru_cache with instance methods: release object
How can I use functools.lru_cache inside classes without leaking memory? In the following minimal example the foo instance won’t be released although going out of scope and having no referrer (other than the lru_cache). But foo and hence foo.big (a BigClass) are still alive That means that Foo/BigClass instances are still residing in memory. Even deleting Foo (del Foo) will
How to use memory_profiler (python module) with class methods?
I want to profile time and memory usage of class method. I didn’t find an out of box solution for this (are there such modules?), and I decided to use timeit for time profiling and memory_usage from memory_profiler module. I faced a problem of profiling methods with memory_profiler. I’ve tried different variants, and none of them worked. When I try