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

281 questions
741
votes
6 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
  • 164,889
  • 61
  • 203
  • 255
219
votes
7 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 =…
54
votes
3 answers

Python functools lru_cache with class 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
  • 713
  • 1
  • 6
  • 7
50
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
  • 819
  • 1
  • 8
  • 12
42
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
  • 2,562
  • 1
  • 23
  • 37
30
votes
4 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,121
  • 11
  • 18
25
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
  • 19,150
  • 4
  • 64
  • 78
20
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
  • 25,410
  • 16
  • 132
  • 172
20
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
  • 297
  • 2
  • 8
19
votes
7 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 =…
18
votes
3 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
  • 760
  • 8
  • 20
17
votes
2 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
  • 3,898
  • 3
  • 23
  • 38
14
votes
1 answer

What exactly is the optimization `functools.partial` is making?

CPython 3.6.4: from functools import partial def add(x, y, z, a): return x + y + z + a list_of_as = list(range(10000)) def max1(): return max(list_of_as , key=lambda a: add(10, 20, 30, a)) def max2(): return max(list_of_as ,…
14
votes
5 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
  • 63,678
  • 30
  • 130
  • 206
13
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
  • 766
  • 7
  • 21
1
2 3
18 19