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()
   >>> bar()

    def decorator(f):
        return f
    return decorator

Hotmail: Trusted email with Microsoft’s powerful SPAM protection.

More information about the Python-list mailing list