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
15
votes
1 answer

Proper type hint for functools.partial

What's the proper type hint for functools.partial? I have a function that returns a partial and I want to type hint it so mypy doesn't throw any error: def my_func() -> ?: return partial(foo, bar="baz") More specific than typing.Callable
A23149577
  • 2,045
  • 2
  • 40
  • 74
15
votes
1 answer

Python multiprocessing - Why is using functools.partial slower than default arguments?

Consider the following function: def f(x, dummy=list(range(10000000))): return x If I use multiprocessing.Pool.imap, I get the following timings: import time import os from multiprocessing import Pool def f(x, dummy=list(range(10000000))): …
usual me
  • 8,338
  • 10
  • 52
  • 95
13
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 ,…
pawelswiecki
  • 562
  • 1
  • 4
  • 14
12
votes
2 answers

How to compare wrapped functions with functools.partial?

If I define my function as below: def myfunc(arg1, arg2): pass then myfunc == myfunc will return True But functools.partial(myfunc, arg2=1) == functools.partial(myfunc, arg2=1) will return False. For unittest purpose, is there an easy way to…
Lydia
  • 2,377
  • 16
  • 13
12
votes
2 answers

Function decorated using functools.wraps raises TypeError with the name of the wrapper. Why? How to avoid?

def decorated(f): @functools.wraps(f) def wrapper(): return f() return wrapper @decorated def g(): pass functools.wraps does its job at preserving the name of g: >>> g.__name__ 'g' But if I pass an argument to g, I get a…
jacquev6
  • 620
  • 4
  • 18
12
votes
1 answer

python mock: @wraps(f) problems

I want to test a simple decorator I wrote: It looks like this: #utilities.py import other_module def decor(f): @wraps(f) def wrapper(*args, **kwds): other_module.startdoingsomething() try: return f(*args,…
powlo
  • 2,538
  • 3
  • 28
  • 38
11
votes
1 answer

Why does functools.lru_cache break this function?

Consider the following function, which returns all the unique permutations of a set of elements: def get_permutations(elements): if len(elements) == 0: yield () else: unique_elements = set(elements) for first_element…
user76284
  • 1,269
  • 14
  • 29
11
votes
2 answers

functools.wraps won't let me wrap a function with a class in Python 3

I want to write a decorator for some functions that take file as the first argument. The decorator has to implement the context manager protocol (i.e. turn the wrapped function into a context manager), so I figured I needed to wrap the function with…
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
10
votes
2 answers

What is the unit in python lru_cache?

According to the documentation the default value for lru_cache from functools is 128. But no unit is defined. Decorator to wrap a function with a memoizing callable that saves up to the maxsize most recent calls. It can save time when an…
Soerendip
  • 7,684
  • 15
  • 61
  • 128
10
votes
3 answers

Pandas 'reduce' and 'accumulate' functions - incomplete implementation

I would like to use reduce and accumulate functions in Pandas in a way similar to how they apply in native python with lists. In itertools and functools implementations, reduce and accumulate (sometimes called fold and cumulative fold in other…
NBF
  • 201
  • 1
  • 2
  • 5
10
votes
1 answer

Functools.update_wrapper() doesn't work properly

I use Functools.update_wrapper() in my decorator, but It seems like update_wrapper rewrites only function attributes (such as __doc__, __name__), but does not affect on help() function. I aware of these answers, but they don't work with…
prybalko
  • 115
  • 1
  • 7
10
votes
1 answer

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…
rominf
  • 2,719
  • 3
  • 21
  • 39
9
votes
4 answers

Python library functions taking no keyword arguments

This problem originated when I tried to apply a more functional approach to problems in python. What I tried to do is simply square a list of numbers, no biggie. from operator import pow from functools import partial squared =…
9
votes
2 answers

Using functools.lru_cache on functions with constant but non-hashable objects

Is it possible to use functools.lru_cache for caching a partial function created by functools.partial? My problem is a function that takes hashable parameters and contant, non-hashable objects such as NumPy arrays. Consider this toy example: import…
cel
  • 30,017
  • 18
  • 97
  • 117
9
votes
3 answers

One liner to look up nested value from a dictionary python

Suppose I have a dictionary that is nested arbitrarily: d = { 11: { 21: {31: 'a', 32: 'b'}, 22: {31: 'a', 34: 'c'}, }, 12: { 1: {2: 3} } } And a list of keys whose position tells me which nested dictionary to…
Bahrom
  • 4,752
  • 32
  • 41
1
2
3
25 26