C-style static variables in Python?

Lee Harr missive at hotmail.com
Mon Apr 5 17:04:14 EDT 2010


> Another approach would be to stuff the static values in the function's
> __dict__.

That's how I did it when I wanted something similar.

I created this decorator:


def static(**kw):
    '''
    Used to create a decorator function that will add an
    attribute to a function and initialize it.

   >>> @static(foo=5)
    ... def bar():
    ...     print bar.foo
    ...     bar.foo += 1
    ...
   >>> bar()
    5
   >>> bar()
    6
    '''

    def decorator(f):
        f.__dict__.update(kw)
        return f
    return decorator

 		 	   		  
_________________________________________________________________
Hotmail: Trusted email with Microsoft’s powerful SPAM protection.
https://signup.live.com/signup.aspx?id=60969



More information about the Python-list mailing list