Pythonic way to do static local variables?
Michael Spencer
mahs at telcopartners.com
Tue Apr 26 13:26:51 EDT 2005
Terry Reedy wrote:
> "Charles Krug" <cdkrug at worldnet.att.net> wrote in message
> news:7Ujbe.647694$w62.444038 at bgtnsc05-news.ops.worldnet.att.net...
>
>>Both of these techniques look promising here.
>
>
> Here a third, the closure approach (obviously not directly tested):
>
>
>
Just for grins, here's a fourth approach, using the descriptor protocol
>>> def func_with_state(state,a):
... state.append(a)
... return state
...
>>> f = func_with_state.__get__([])
>>> f(1)
[1]
>>> f(2)
[1, 2]
>>> f(3)
[1, 2, 3]
>>>
Michael
More information about the Python-list
mailing list