functools

Python3 pass lists to function with functools.lru_cache

Python3 pass lists to function with functools.lru_cache Question: I want to cache a function that takes a list as a parameter, but when I try to do so with the functools.lru_cache decorator, it fails with TypeError: unhashable type: ‘list’. import functools @functools.lru_cache() def example_func(lst): return sum(lst) + max(lst) + min(lst) print(example_func([1, 2])) Asked By: redfast00 …

Total answers: 5

python equivalent of functools 'partial' for a class / constructor

python equivalent of functools 'partial' for a class / constructor Question: I want to create a class that behaves like collections.defaultdict, without having the usage code specify the factory. EG: instead of class Config(collections.defaultdict): pass this: Config = functools.partial(collections.defaultdict, list) This almost works, but isinstance(Config(), Config) fails. I am betting this clue means there are …

Total answers: 5

Python functools lru_cache with instance methods: release object

Python functools lru_cache with instance methods: release object Question: 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). from functools import lru_cache class BigClass: pass class Foo: def __init__(self): self.big …

Total answers: 9

Python mock.patch.object with functool.partial bound arguments possible?

Python mock.patch.object with functool.partial bound arguments possible? Question: How to solve this? Patch a objects method with another signature (eg. an additional argument. I’ve tried to bound the optional argument, but this does not seem to work. I can not use plain monkey patching here, since the the patched class is called under in a …

Total answers: 3

Make @lru_cache ignore some of the function arguments

Make @lru_cache ignore some of the function arguments Question: How can I make @functools.lru_cache decorator ignore some of the function arguments with regard to caching key? For example, I have a function that looks like this: def find_object(db_handle, query): # (omitted code) return result If I apply lru_cache decorator just like that, db_handle will be …

Total answers: 2

Why should I set the __doc__ of a partial object in Python?

Why should I set the __doc__ of a partial object in Python? Question: help() doesn’t show the __doc__ of a partial object. Yet, the example in the docs sets it: >>> from functools import partial >>> basetwo = partial(int, base=2) >>> basetwo.__doc__ = ‘Convert base 2 string to an int.’ >>> basetwo(‘10010’) 18 Why set …

Total answers: 2

functools.partial on class method

functools.partial on class method Question: I’m trying to define some class methods using another more generic class method as follows: class RGB(object): def __init__(self, red, blue, green): super(RGB, self).__init__() self._red = red self._blue = blue self._green = green def _color(self, type): return getattr(self, type) red = functools.partial(_color, type=’_red’) blue = functools.partial(_color, type=’_blue’) green = functools.partial(_color, …

Total answers: 2

How does functools partial do what it does?

How does functools partial do what it does? Question: I am not able to get my head on how the partial works in functools. I have the following code from here: >>> sum = lambda x, y : x + y >>> sum(1, 2) 3 >>> incr = lambda y : sum(1, y) >>> incr(2) …

Total answers: 8

itertools.accumulate() versus functools.reduce()

itertools.accumulate() versus functools.reduce() Question: In Python 3.3, itertools.accumulate(), which normally repeatedly applies an addition operation to the supplied iterable, can now take a function argument as a parameter; this means it now overlaps with functools.reduce(). With a cursory look, the main differences between the two now would seem to be: accumulate() defaults to summing but …

Total answers: 3

Differences between functools.partial and a similar lambda?

Differences between functools.partial and a similar lambda? Question: In Python, suppose I have a function f that I want to pass around with some secondary arguments (assume for simplicity that it’s just the first argument that remains variable). What are the differences between doing it these two ways (if any)? # Assume secondary_args and secondary_kwargs …

Total answers: 6