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. Advertisements

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:
    493
    Tom Yates
    Oct 20, 2003
  2. amine  zejli
    Replies:
    0
    Views:
    462
    amine zejli
    Oct 19, 2003
  3. Amine Zejli
    Replies:
    1
    Views:
    512
    William Ryan
    Oct 19, 2003
  4. Brian W
    Replies:
    10
    Views:
    1,004
    Brian W
    Jul 2, 2003
  5. George Sakkis

    Injecting code into a function

    George Sakkis, Apr 25, 2005, in forum: Python
    Replies:
    17
    Views:
    670
    Lonnie Princehouse
    Apr 26, 2005
  6. Oodini
    Replies:
    1
    Views:
    2,037
    Keith Thompson
    Sep 27, 2005
  7. Replies:
    1
    Views:
    670
    Sion Arrowsmith
    Jul 10, 2008
  8. Alok
    Replies:
    3
    Views:
    472
Loading...