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

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -