Just as a dynamic class can be created using type(name, base-classes, namespace-dict), can a dynamic function be created?
I\'ve tried doing something along the line
Yes. Use the def
statement:
def functor(f): # this dynamically creates a function at module load time
def inner(g): #this dynamically creates a function on execution of functor
return f(g)
return inner
Any solution involving freestanding text being compiled is equivalent to exec
or eval
, which leaves you with pre-existing functions and lexically captured data items to stitch together new functions. This might give you some ideas.