Here is a way to do this when x is global. I don't recommend it, because it modifies the global dictionary. Still it gives an effect closes to what you want perhaps.
>>>incr = lambda x: globals().__setitem__('x',x+1) >>>def f(): incr(x); return x >>>x=10 >>>f() 11 >>>f() 12 >>>x 12 --Anand On Feb 18, 2008 5:57 PM, Siddharta <[EMAIL PROTECTED]> wrote: > Pythonic wrote: > > * 2. Best solution (Pythonic)* > > > > > >>>> import itertools > >>>> c = itertools.count(9) > >>>> c.next() > >>>> > > 9 > > > >>>> c.next() > >>>> > > 10 > > > > +1 Pythonic: itertools is the way to go > > But if you really want to implement it yourself, you need to do > something like this - > > >>> class ns: pass > ... > >>> def make_incr(start): > ... v = ns() > ... v.count = start > ... def incr(): > ... v.count += 1 > ... return v.count > ... return incr > ... > >>> i = make_incr(5) > >>> i() > 6 > >>> i() > 7 > > It's more complex than if you did it in a functional language because of > limitations on lambda and accessing variables in nested scopes. > > -- > Siddharta Govindaraj > > _______________________________________________ > BangPypers mailing list > BangPypers@python.org > http://mail.python.org/mailman/listinfo/bangpypers > -- -Anand _______________________________________________ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers