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

How to test that functools.partial produces the expected function object

When going from one API to another it can sometimes be helpful to map between similar keywords in each API, allowing one controller API to flexibly dispatch to other libraries without needing the user to fuss around with the different API's under…
ely
  • 74,674
  • 34
  • 147
  • 228
5
votes
4 answers

How to apply a list of functions sequentially to a string using Python reduce or list comprehension?

Problem Statement I would like to apply a list of functions fs = [ f, g, h ] sequentially to a string text=' abCdEf ' Something like f( g( h( text) ) ). This could easily be accomplished with the following code: # initial text text = ' abCDef …
5
votes
3 answers

Creating a dynamic partial callable object in Python

I have a regular expression defined in a YAML configuration file. To make things easier, I'll use a dictionary here instead: rule_1 = { 'kind': 'regex', 'method': 'match', 'args': None, 'kwargs': { 'pattern': "[a-z_]+", …
muxevola
  • 199
  • 1
  • 2
  • 10
5
votes
1 answer

Python - how do I memoize a partial object?

I have a set of functions that take integers and functions as arguments. I'd like to memoize them. I know that using this solution, I could use pickle to encode both sets of arguments and memoize the encoded values. In this particular use case,…
MikeRand
  • 4,788
  • 9
  • 41
  • 70
5
votes
2 answers

Using partial with a conditional default argument

I'm using partial on a library function to supply it with a default value for one of the parameters library_func = lambda x, y, z : x + y + z my_func = functools.partial(library_func, z = 5) #default value for one of the variables # equiv to lambda…
Greedo
  • 4,967
  • 2
  • 30
  • 78
5
votes
2 answers

error while installing python functools32 on ubuntu 16.04 using python3.7

I tried to install python functools on ubuntu 16.04 machine and I recieved this error Collecting functools32 Downloading…
Prasad
  • 51
  • 1
  • 3
5
votes
1 answer

Using functools.partial within class structure, "name 'self' is not defined"

Below is a significantly simplified version on my code. After the __init__() there are several functions. I am trying to use functools.partial to create different versions of a basic comparison function, which references a function created earlier…
Emily K
  • 230
  • 4
  • 14
5
votes
1 answer

is not a Python function

I'm trying to build a function which I can use as a handler for an RxPy stream that I'm mapping over. The function I have needs access to a variable outside the scope where that variable is defined which, to me, means that I need to use a closure of…
Justin Valentini
  • 426
  • 3
  • 14
5
votes
1 answer

Python functools.lru_cache eviction callback or equivalent

Is it possible to define a callback for functools.lru_cache when an item is evicted? In the callback the cached value should also be present. If not, maybe someone knows a light-weight dict-like cache that supports eviction and callbacks?
Iulian
  • 1,496
  • 2
  • 15
  • 35
5
votes
1 answer

functools.partial and generators

I am trying do the following: import functools class TestClass(): def method(self, n): for i in xrange(n): yield i # This works for x in TestClass().method(10): print x # This gets a TypeError: functools.partial…
Ruediger Jungbeck
  • 2,836
  • 5
  • 36
  • 59
5
votes
2 answers

Why should I set the __doc__ of a partial object in Python?

help() doesn't show the __doc__ of a partial object. Yet, the example in the docs sets it: >>> from functools import partial >>> basetwo = partial(int, base=2) >>> basetwo.__doc__ = 'Convert base 2 string to an int.' >>> basetwo('10010') 18 Why set…
Chris Wesseling
  • 6,226
  • 2
  • 36
  • 72
5
votes
1 answer

Python multiprocessing map function error

I have a simple multiprocessing example that I'm trying to create. The ordinary map() function version works, but when changed to Pool.map, I'm getting a strange error: from multiprocessing import Pool from functools import partial x = [1,2,3] y =…
4
votes
1 answer

More elegant way to switch tqdm progress bars without LSP Pyright error

I'm creating a custom Python CLI module using docx2pdf. The docx2pdf module is using tqdm to display progress bars while the .docx files are being converted to .pdf. In my module I'm using the CLI log parameter to enable/disable console logging…
4
votes
1 answer

functools.singledispatchmethod with own class as arg type

I would like to use functools.singledispatchmethod to overload the binary arithmetic operator methods of a class called Polynomial. The problem I have is that I can't find a way to register method calls where other is a Polynomial. Perhaps better…
Vinzent
  • 1,070
  • 1
  • 9
  • 14
4
votes
0 answers

How to dynamically set the maxsize of python lru_cache [duplicated]

I would like to dynamically set the maxsize of python lru_cache, but when I wrap a function (A._get) in antoher function (A.get), the cache doesn't work. The original use of lru_cache (A._get2) works properly. from functools import partial,…
wuya
  • 85
  • 1
  • 6