function local namespace question

Miles Kaufmann milesck at umich.edu
Wed Jul 8 16:50:44 EDT 2009


On Jul 8, 2009, at 1:35 PM, Paul LaFollette wrote:

> I cannot figure out any way to get a hook into the local namespace of
> a user defined function.  I have tried making a wrapper class that
> grabs the function call and then uses exec to invoke
> myfunction.__code__ with my own dictionaries.  This runs the (no
> argument) function properly (losing the return value, but I can deal
> with that) but never accesses the local logging dictionary that I
> specify in the exec() call. Perhaps the local namespace of a function
> is not a dict at all?

Right.  Within functions, local variable name accesses and assignments  
are compiled into bytecode instructions (LOAD_FAST, STORE_FAST) that  
manipulate pointers to objects by indexing directly into a C array in  
the frame.  The "locals" dictionary of a frame is generated on-demand  
from that array when accessed.  There is no way that I'm aware of to  
directly hook into function namespace access and manipulation.

-Miles




More information about the Python-list mailing list