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

How to convert `lambda` object to `function` object for pickling in Python?

I've been dealing with errors regarding lambda functions and their inability to be pickled. I often use lambda functions on the fly as one time use functions and it vastly decreases my workflow productivity when I have to individually recreate…
O.rka
  • 29,847
  • 68
  • 194
  • 309
7
votes
0 answers

Remove function argument after setting it to a value with functools.partial

I would like to use functools.partial to set a certain argument to a constant and at the same time remove the argument altogether. Let me explain it using a simple example. from functools import partial def f(a, b): return a * b g = partial(f,…
johnbaltis
  • 1,413
  • 4
  • 14
  • 26
7
votes
3 answers

Does Python have an iterative recursion generator function for first-order recurrence relations?

Is there a built in function or standard library function roughly equivalent to def recur_until(start, step_fu, stop_predicate=lambda _: False): current = start while not stop_predicate(current): yield current current =…
das-g
  • 9,718
  • 4
  • 38
  • 80
7
votes
1 answer

Python counterpart to partial for ignoring an argument

Is there a "counterpart" in python for functools.partial? Namely what I want to avoid is writing: lambda x, y: f(x) But I would love to preserve the same attributes (keyword-args, nice repr) as I do when I write: from functools import partial incr…
dreuter
  • 73
  • 4
6
votes
1 answer

Why does `functools.partial` not inherit `__name__` and other meta data by default?

I am wondering why meta data (e.g. __name__, __doc__) for the wrapped method/function by partial is not inherited by default. Instead, functools provides the update_wrapper function. Why it is not done by default is not mentioned anywhere (as far as…
BenedictWilkins
  • 1,173
  • 8
  • 25
6
votes
2 answers

The rationale of `functools.partial` behavior

I'm wondering what the story -- whether sound design or inherited legacy -- is behind these functools.partial and inspect.signature facts (talking python 3.8 here). Set up: from functools import partial from inspect import signature def bar(a, b): …
thorwhalen
  • 1,920
  • 14
  • 26
6
votes
3 answers

Try each function of a class with functools.wraps decorator

I'm trying to define a decorator in order to execute a class method, try it first and, if an error is detected, raise it mentioning the method in which failed, so as to the user could see in which method is the error. Here I show a MRE (Minimal,…
Miguel Gonzalez
  • 706
  • 7
  • 20
6
votes
1 answer

Why does functools.update_wrapper update the __dict__ in the wrapper object?

I came across a peculiar behaviour of functools.update_wrapper: it overwrites the __dict__ of the wrapper object by that of the wrapped object - which may hinder its use when nesting decorators. As a simple example, assume that we are writing a…
Andi Kleve
  • 135
  • 4
6
votes
1 answer

What is python's functools.cmp_to_key returning?

I get that as in the docs, I can use cmp_to_key to convert the old style comparison from python2 to a key function such as in python3, but I don't get what exactly is being returned. For instance: def my_cmp(a, b): if(a == b): return 0 …
guptashark
  • 119
  • 7
6
votes
1 answer

deploying python flask app on heroku gives error with functools32

First question on stackoverflow :D (because I found almost everything until now). I try to deploy my python app to Heroku, but the following error appears: git push heroku master Counting objects: 7036, done. Compressing objects: 100% (3933/3933),…
Si Mon
  • 397
  • 3
  • 16
6
votes
3 answers

functools.wrapper - AttributeError: attribute '__doc__' of 'type' objects is not writable

While executing the code below, I'm getting AttributeError: attribute '__doc__' of 'type' objects is not writable. from functools import wraps def memoize(f): """ Memoization decorator for functions taking one or more arguments. Saves…
user2264738
  • 302
  • 4
  • 18
6
votes
1 answer

How are Python2's built-in reduce and functools.reduce different?

Both have exact same documentation, and it seems to me that both stem from same source code in https://hg.python.org/cpython/file/Modules/_functoolsmodule.c. However, I am not sure about it. I didn't find any other refernces in the source code of…
oz123
  • 27,559
  • 27
  • 125
  • 187
6
votes
3 answers

Python functools.partial - How to apply it to a class method with the static decorator

I know there must be a way to do this. But I'm getting the error "TypeError: the first argument must be callable". What could I do differently to make this work? class FaxMachine(object): MODEL_NO = '100' @staticmethod def…
Greg
  • 45,306
  • 89
  • 231
  • 297
6
votes
3 answers

Logging execution time with decorators

After I tried unsuccessfully for a while, I am seeking help from this miraculous website. Now for my problem: I want to create a decorator that writes the elapsed execution time of a function (during the execution of the function) into a logging…
Kaleidophon
  • 589
  • 1
  • 5
  • 16
6
votes
1 answer

how to partially apply arbitrary argument of a function?

I want to use partial from functools to partially apply a function's second argument, I know it is easy to do with lambda rather than partial as follows >>> def func1(a,b): ... return a/b ... >>> func2 = lambda x:func1(x,2) >>> func2(4) 2 but I…
Kavin Eswaramoorthy
  • 1,595
  • 11
  • 19