Implementation of the global statement

Greg Ewing see_reply_address at something.invalid
Wed Nov 27 23:41:23 EST 2002


Mikael Olofsson wrote:

>   Is there an obvous way to have f (still defined in A) manipulate
>   objects in B when executed in B?


Don't know whether it counts as "obvious", but I
think there is a way (short of using exec):

   import new
   g = new.function(f.func_code, B.__dict__)

This creates a new function g which has the
same code as f but a different global namespace.

(You can't just change f.func_globals directly
because it's a read-only attribute, unfortunately.)

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list