Injecting a global into a defined function??

Discussion in 'Python' started by Cong Ma, Jan 15, 2009.

  1. Cong Ma

    Cong Ma Guest

    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.
    Cong Ma, Jan 15, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. amine  zejli
    Replies:
    1
    Views:
    405
    Tom Yates
    Oct 20, 2003
  2. George Sakkis

    Injecting code into a function

    George Sakkis, Apr 25, 2005, in forum: Python
    Replies:
    17
    Views:
    450
    Lonnie Princehouse
    Apr 26, 2005
  3. Oodini
    Replies:
    1
    Views:
    1,735
    Keith Thompson
    Sep 27, 2005
  4. Replies:
    1
    Views:
    473
    Sion Arrowsmith
    Jul 10, 2008
  5. Alok
    Replies:
    3
    Views:
    233
Loading...

Share This Page