ARTICLE AD BOX
I use the "decorator" idea as way to parameterize functions, but ran into pickling errors when passing these functions as targets to the multiprocessing module. I found a work-around by setting the __qualname__ attribute of the wrapper function explicitly (overriding the name set using functools.wraps.
Is this a legitimate thing to do? Or will I run into issues with this idea?
from functools import wraps from math import cos, pi def parameterize_f(*args): def decorator(func,qname): @wraps(func) def wrapper(x): return func(x,*args) # Needed for pickling. wrapper.__qualname__ = qname # <--------- override wraps setting above? return wrapper return decorator def print_results(results): for r in results: print(r) def f_linear(x,a,b): return a*x + b def f_trig(x,m): return cos(2*pi*m*x) # parameterized function fp = parameterize_f(a := 2, b := 3)(f_linear,'fp') # fp = parameterize_f(m := 1)(f_trig,'fp') print(fp(0.125))