Injecting a global into a defined function??

Cong Ma cma at mail.bnu.edu.cn
Thu Jan 15 13:28:35 EST 2009


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.

Regards,
Cong.




More information about the Python-list mailing list