[Tutor] Re: C-like local static var in Py ?

Bob Gailer ramrom@earthling.net
Sun Dec 1 22:02:00 2002


At 07:56 PM 12/1/2002 -0700, Bob Gailer wrote:
> >>> def inc():
>...     if inc.__dict__.has_key('a'):
>...             inc.__dict__['a'] += 1
>...     else:
>...             inc.__dict__['a'] = 1
>...     print inc.__dict__['a']

I realize that this is a bit of overkill: A simpler version:

 >>> def inc():
...     try: inc.a += 1
...     except: inc.a = 1
...     print inc.a

Bob Gailer
mailto:ramrom@earthling.net
303 442 2625