Ramaswamy, Vivek wrote: > Hello~ > > I am relatively new to python, but I was trying to auto increment a > variable through using the lambda anonymous function, but unfortunately > I am un-able to get the desired result. Please let me know where I am > going wrong > g=lambda m:lambda: ++m > incre=g(0) > incre() > > 0 > incre() > > 0 > ++ operator is not supported in Python. Assignment would not work in a lambda. You may solve it using one of the approaches below
* 1. Simple (to understand) solution: * >>> m = 0 >>> def mIncrementer(): ... global m ... m += 1 ... return m >>> mIncrementer() 1 >>> mIncrementer() 2 * 2. Best solution (Pythonic)* >>> import itertools >>> c = itertools.count(9) >>> c.next() 9 >>> c.next() 10 _______________________________________________ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers