How to get dynamically-created fxn's source?

gb345 gb345 at invalid.com
Mon Nov 8 11:45:24 EST 2010


In <ib1fai$8g3$02$1 at news.t-online.com> Peter Otten <__peter__ at web.de> writes:

>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])

>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>

I was not aware of functools.partial.  This makes the problem a
lot easier.  Thanks!

G



More information about the Python-list mailing list