Is there a function that applies list of functions to a value?
Terry Reedy
tjreedy at udel.edu
Fri Aug 30 00:38:55 EDT 2013
On 8/29/2013 5:48 PM, fp2161 at gmail.com wrote:
> Here is the generalisable version:
>
> def comp(*func):
> def anon(x):
> res=x
> for f in func:
> res=f(res)
> return res
> return anon
With a bit more work, one can set the __name__ and __qualname__ attributes.
import math as m
def comp(*func, name=''):
def anon(x):
res=x
for f in func:
res=f(res)
return res
if name:
anon.__name__ = name
q = anon.__qualname__.rsplit('.', maxsplit=1)
q[1] = name
anon.__qualname__ = '.'.join(q)
return anon
esincos = comp(m.exp, m.sin, m.cos, name='esincos')
print(esincos)
#
<function comp.<locals>.esincos at 0x00000000033107B8>
--
Terry Jan Reedy
More information about the Python-list
mailing list