python - Simple number generator -
how write function returns incrementing numbers on each call?
print counter() # 0 print counter() # 1 print counter() # 2 print counter() # 3 etc
for clarity, i'm not looking generator (although answer use it). function should return integer, not iterable object. i'm not looking solution involving global or otherwise shared variables (classes, function attribute etc).
see here http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-syntax.html useful insights matter.
in short,
from itertools import count counter = lambda c=count(): next(c)
Comments
Post a Comment