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 lines of:
>>> f = type("f", (function,), {})
NameError: name 'function' is not defined
Ok, so I'll be clever, but:
>>> def fn():
... pass
...
>>> type(fn)
<type 'function'>
>>> f = type("f", (type(fn),), {})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: type 'function' is not an acceptable base type
Does Python specifically prevent the creation of dynamic functions in the same way it allows dynamic classes?
Edit: Note, I'd disallow any use of exec.. Since my question is does the Python language itself permit this.
Thanks in advance.
See Question&Answers more detail:os