Self-referencing decorator function parameters

Erich sophacles at gmail.com
Wed Apr 2 17:41:10 CEST 2008


On Apr 2, 10:13 am, Thomas Dimson <tdim... at gmail.com> wrote:
>
> I guess my real question is: why does wrapping the call to be
> "call=lambda x: DecorateMe.callMe(x)" somehow fix the issue with this
> temporary namespace? It seems strange to me that defining an
> additional function (through lambda) would allow me to see/add more
> members to the namespace.

This works because of the very same mechanism that allows decorators
to work.  When the decorator in your example is called, it then
evaluates the inner statements, including the creation of the function
inner (the def ...).  Similarly, when you do the lambda above, python
points call to the lambda function, but does not evaluate it until
later, when the youBet method is run. By the time youBet is run, the
DecorateMe class exists, so this will properly evaluate.

I hope the above is clear, I haven't had my coffee yet.

regards,
Erich



More information about the Python-list mailing list