Static Variables

Delaney, Timothy tdelaney at avaya.com
Tue Apr 2 23:24:46 EST 2002


> From: Gerhard Häring [mailto:gerhard at bigfoot.de]
> 
> Jim Jacobs wrote in comp.lang.python:
> > Is it possible to simulate "C" style static variables in functions?
> 
> No.
> 
> > In methods?
>  
> No.

Of course it's possible.

def function (a, b, c=[]):
    pass

c is a static variable (however, could be overridden by the caller).

> This concept sucks anyway, IMNSHO.

Generally, yes, but it can be very useful. For example, for function-level
synchronisation (ignoring the use of Queue.Queue here ;)

def function (a, b, lock=threading.RLock()):
    
    lock.acquire()

    try:
        pass
    finally:
        lock.release()

Tim Delaney




More information about the Python-list mailing list