Local static variables
Posted: Sun May 14, 2017 12:41 pm
Hi,
I am trying to create a local static variable in a function that will be used as a callback. I have tried the following code, which I know works in Python 2.7 and above, but won't work in MicroPython as it does not support user-defined attributes.
def foo():
if not hasattr(foo, "counter"):
print ("True")
foo.counter = 1
else:
print("False")
foo.counter += 1
print (foo.counter)
I would be grateful if anyone can suggest an alternative way of creating local static variables in a MicroPython function.
Regards,
Mervyn
I am trying to create a local static variable in a function that will be used as a callback. I have tried the following code, which I know works in Python 2.7 and above, but won't work in MicroPython as it does not support user-defined attributes.
def foo():
if not hasattr(foo, "counter"):
print ("True")
foo.counter = 1
else:
print("False")
foo.counter += 1
print (foo.counter)
I would be grateful if anyone can suggest an alternative way of creating local static variables in a MicroPython function.
Regards,
Mervyn