Injecting a global into a defined function??

MRAB google at mrabarnett.plus.com
Thu Jan 15 19:43:31 CET 2009


Cong Ma wrote:
> Hi,
> 
> I'd appreciate your hints on this problem. I'm writing a module in which several
> functions can alter the value of a global variable (I know this sounds evil,
> please forgive me...). What I'm trying to do is to eliminate the "global foo"
> lines in those functions' bodies and to use a decorator for the same task. For
> example:
> 
>     @global_injected("SPAM")
>     def foo():
>         ... ...
> 
> will have the same effect as
> 
>     def foo():
>         global SPAM
>         ... ...
> 
> Leaving the evilness of globals aside, I wonder how I can implement this (for
> Python 2.x). I'd like to hear your opinions. Thank you.
> 
I doubt that's possible because 'global' is syntax.



More information about the Python-list mailing list