static variables
Marko Rauhamaa
marko at pacujo.net
Wed Dec 2 05:18:56 EST 2015
Antoon Pardon <antoon.pardon at rece.vub.ac.be>:
> def foo()
> foo.attr
>
> changes nothing about foo.attr being globally accessible.
I don't know why global accessibility is such a problem.
Anyway, in practice I handle such "static" variables as module globals.
If you want a more puristic solution, you could do:
def _make_f():
x = 0
def f():
nonlocal x
x += 1
return x
return f
f = _make_f()
print(f())
=> 1
print(f())
=> 2
print(f())
=> 3
Marko
More information about the Python-list
mailing list