Questions tagged [functools]

functools is a module for the Python language which provides support for working with higher-order functions: functions that act on or return other functions

functools is a module for the Python language which provides support for working with higher-order functions: functions that act on or return other functions.

The official documentation can be found here: here

382 questions
959
votes
7 answers

What does functools.wraps do?

In a comment on this answer to another question, someone said that they weren't sure what functools.wraps was doing. So, I'm asking this question so that there will be a record of it on StackOverflow for future reference: what does functools.wraps…
Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256
328
votes
8 answers

How does functools partial do what it does?

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) 3 >>> def sum2(x, y): return x + y >>> incr2 =…
89
votes
9 answers

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). from functools import…
televator
  • 1,133
  • 1
  • 9
  • 8
78
votes
2 answers

Make @lru_cache ignore some of the function arguments

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…
WGH
  • 3,222
  • 2
  • 29
  • 42
65
votes
2 answers

functools.partial on class method

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 …
Arjor
  • 979
  • 1
  • 8
  • 12
48
votes
5 answers

Python3 pass lists to function with functools.lru_cache

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): …
redfast00
  • 1,363
  • 1
  • 12
  • 23
48
votes
5 answers

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

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,…
Evan Benn
  • 1,571
  • 2
  • 14
  • 20
35
votes
3 answers

Use functools' @lru_cache without specifying maxsize parameter

The documentation for lru_cache gives the function definition: @functools.lru_cache(maxsize=128, typed=False) This says to me that maxsize is optional. However, it doesn't like being called without an argument: Python 3.6.3 (default, Oct 24 2017,…
Tom Hale
  • 40,825
  • 36
  • 187
  • 242
31
votes
3 answers

itertools.accumulate() versus functools.reduce()

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…
JAB
  • 20,783
  • 6
  • 71
  • 80
29
votes
6 answers

Differences between functools.partial and a similar lambda?

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)? #…
ely
  • 74,674
  • 34
  • 147
  • 228
27
votes
2 answers

Python functools partial efficiency

I have been working with Python and I set up the following code situation: import timeit setting = """ import functools def f(a,b,c): pass g = functools.partial(f,c=3) h = functools.partial(f,b=5,c=3) i =…
user2515310
  • 357
  • 3
  • 8
26
votes
4 answers

What is the difference between partial and partialmethod?

I found out that functools module of Python 3 has two very similar methods: partial and partialmethod. Can someone provide good examples of using each one?
grundic
  • 4,641
  • 3
  • 31
  • 47
24
votes
6 answers

Dynamically created method and decorator, got error 'functools.partial' object has no attribute '__module__'

I am currently using EndpointsModel to create a RESTful API for all my models on AppEngine. Since it is RESTful, these api have a lot of repeat code which I want to avoid. For example: class Reducer(EndpointsModel): name =…
23
votes
2 answers

ImportError: cannot import name 'cache' from 'functools'

All the solutions I managed to find are only on lru_cache. But in my case dir(functools) shows that that very lru_cache does reside in functools while cache does not! How can I solve this?
Kaiyakha
  • 1,463
  • 1
  • 6
  • 19
19
votes
2 answers

Cannot import name 'MappingProxyType' error after importing functools

After I import functools I receive such message from interpreter: Traceback (most recent call last): File "C:/Users/Admin/Documents/Python/decorator.py", line 1, in import functools File "C:\Python3\lib\functools.py", line 22, in …
Kirill Korolev
  • 966
  • 3
  • 9
  • 22
1
2 3
25 26