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
9
votes
2 answers

implementing functools.partial that prepends additional arguments

The documentation for functools.partial says that it is "roughly equivalent to": def partial(func, *args, **keywords): def newfunc(*fargs, **fkeywords): newkeywords = keywords.copy() newkeywords.update(fkeywords) return…
Dave
  • 7,555
  • 8
  • 46
  • 88
8
votes
3 answers

Test function with lru_cache decorator

I'm attempting to test a a method that is memoized through lru_cache (since it's an expensive database call). with pytest-mock. A simplified version of the code is: class User: def __init__(self, file): # load a file @lru_cache …
adrpino
  • 960
  • 8
  • 33
8
votes
2 answers

functools has no attribute lru_cache

I'm using Python 3.7 on my Windows This error occurs while running every code: Traceback (most recent call last): File "test.py", line 1, in import nltk File…
F.Sh
  • 89
  • 1
  • 2
8
votes
2 answers

How can I add keyword arguments to a wrapped function in Python 2.7?

I first want to stress that I have searched both the web generally and the Python documentation + StackOverflow specifically very extensively and did not manage to find an answer to this question. I also want to thank anyone taking the time to read…
8
votes
5 answers

Usage for lru cache in functools

I want to use lru_cache in my code, however, I get this error: NameError: name 'lru_cache' is not defined I do have an import functools in my code but that does not help Example code is…
user308827
  • 21,227
  • 87
  • 254
  • 417
8
votes
1 answer

Why does my LRU cache miss with the same argument?

I have some code that looks like this: from functools import lru_cache @lru_cache() def get_cheese(type): print('{}? We\'re all out.'.format(type)) return…
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
8
votes
1 answer

Equivalent to super() for functools.singledispatch

functools.singledispatch helps to define a single-dispatch generic method. Meanwhile, there is super() for calling methods or accessing attributes of a superclass. Is there something like super() that can be used with singledispatch? I tried the…
minhee
  • 5,688
  • 5
  • 43
  • 81
8
votes
3 answers

Custom sorting on a namedtuple class

I use namedtuple classes a lot. I have been thinking today if there is a nice way to implement custom sorting for such a class, i.e. make the default sort key not the first element (then second, third, etc) of the namedtuple. My first instinct was…
pfctdayelise
  • 5,115
  • 3
  • 32
  • 52
8
votes
1 answer

Error encountered using decorator to update wrapper

I've encountered a rather cryptic (to me at least) error message while trying to use a decorator to update a function's wrapper. Any ideas how I could remedy this? I've tried to make my code as general as possible so it will apply to other…
Madison May
  • 2,723
  • 3
  • 22
  • 32
7
votes
2 answers

Python: Why do functools.partial functions not become bound methods when set as class attributes?

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: from…
7
votes
2 answers

How to use singledispatchmethod with inheritance class

In my code, I have the following class: class A: @functools.singledispatchmethod def handle(arg): pass I want other class to inherit from A and overload the generic method handle like so: class B(A): @handle.register def…
Yedidya kfir
  • 1,419
  • 3
  • 17
  • 32
7
votes
1 answer

Calculating weighted average by GroupBy.agg and a named aggregation

Pandas version 0.25 supports "Named Aggregation" via function agg and namedtuples. You need to pass column, aggregator pairs as the doc describes. It also says: If your aggregation functions require additional arguments, partially apply them…
Ferenc Bodon
  • 310
  • 4
  • 12
7
votes
2 answers

disable `functools.lru_cache` from inside function

I want to have a function that can use functools.lru_cache, but not by default. I am looking for a way to use a function parameter that can be used to disable the lru_cache. Currently, I have a two versions of the function, one with lru_cache and…
TheStrangeQuark
  • 2,257
  • 5
  • 31
  • 58
7
votes
3 answers

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…
Fletch F Fletch
  • 411
  • 5
  • 7
7
votes
2 answers

Clearing lru_cache of certain methods when an attribute of the class is updated?

I have an object with a method/property multiplier. This method is called many times in my program, so I've decided to use lru_cache() on it to improve the execution speed. As expected, it is much faster: The following code shows the problem: from…
agiap
  • 503
  • 1
  • 6
  • 16
1 2
3
25 26