I'm trying to create a self-contained counter function, but if I use a variable it resets each time the function is called, but it works when I use function.attribute.
def turncounter():
try:
turns = turns + 1
except:
turns = 0
try:
turncounter.turn = turncounter.turn + 1
except:
turncounter.turn = 0
print(turns,turncounter.turn)
turncounter()
turncounter()
It returns:
0 0
0 1
Why is that? Aren't function variables and function attributes the same? And is it correct to use function.attribute instead of using variables?