I thought that I could update a variable declared at the top of a Python script from within a function, but that's not true. I manage to do that using global:
count = 0
def updateCount():
global count
print count
count = (count+1)%10
for x in xrange(10):
updateCount()
Is this the best way to handle this ( a function updating a variable at a higher level) ? What is the 'pythonic' way of dealing with this. global seems a bit loose.
Also, please let me know if this is already answered and I'll close the question. I've been reading quite a few answers already posted close to the issue, but not quite there.
count = updateCount(count). For state that must be preserved (and perhaps mutated), consider objects to store such data.updateCountto have a parameter and assigningcount = updateCount(count), or use a mutable object with methods to change its own state (increment acountattribute in your case).