How to get dynamically-created fxn's source?

Peter Otten __peter__ at web.de
Fri Nov 5 13:36:30 EDT 2010


gb345 wrote:

> For a project I'm working on I need a way to retrieve the source
> code of dynamically generated Python functions.  (These functions
> are implemented dynamically in order to simulate "partial application"
> in Python.[1])  The ultimate goal is to preserve a textual record
> of transformations performed on data, along with all the data (both
> pre- and post- transformation) itself.

Are you aware of functools.partial?

>>> from functools import partial
>>> def f(a, b, c, x, y, z):
...     return a*x + b*y*y + c*z*z*z
...
>>> fstar = partial(f, x=1, y=2, z=3)
>>> fstar(1, 0, 0)
1
>>> fstar(0, 1, 0)
4
>>> fstar(0, 0, 1)
27
>>> fstar.args, fstar.keywords
((), {'y': 2, 'x': 1, 'z': 3})
>>> fstar.func
<function f at 0x7fc4868e6848>




More information about the Python-list mailing list