The Python script that does the same thing as the C code you asked is as follows.
def foo():
foo.counter += 1
print "Counter is %d" % foo.counter
foo.counter = 0
To make foo.counter = 0
above the function, you must create a decoder as follows:
def static_vars(**kwargs):
def decorate(func):
for k in kwargs:
setattr(func, k, kwargs[k])
return func
return decorate
@static_vars(counter=0)
def foo():
foo.counter += 1
print "Counter is %d" % foo.counter
def foo():
try:
foo.counter += 1
except AttributeError:
foo.counter = 1
finally:
print("Counter is %d" % foo.counter)
© 2024 OneMinuteCode. All rights reserved.