static variables
Ulli Horlacher
framstag at rus.uni-stuttgart.de
Mon Nov 30 12:15:04 EST 2015
I try to to implement a "static variable" inside a function:
def main():
a(1)
a(2)
a()
print(a.x)
if 'a.x' in globals(): print('global variable')
if 'a.x' in locals(): print('local variable')
def a(x=None):
if not x is None: a.x = x
print(':',a.x)
main()
When I run this code, I get:
: 1
: 2
: 2
2
This is exactly what I expect.
But what is a.x?
It is neither a variable in globals() nor in locals()
--
Ullrich Horlacher Server und Virtualisierung
Rechenzentrum IZUS/TIK E-Mail: horlacher at tik.uni-stuttgart.de
Universitaet Stuttgart Tel: ++49-711-68565868
Allmandring 30a Fax: ++49-711-682357
70550 Stuttgart (Germany) WWW: http://www.tik.uni-stuttgart.de/
More information about the Python-list
mailing list