[Tutor] python closures

Kent Johnson kent37 at tds.net
Tue Dec 1 04:00:01 CET 2009


On Mon, Nov 30, 2009 at 7:50 PM, Dave Angel <davea at ieee.org> wrote:

> And this
> example could be made more complex if outer() is a generator, in which case
> it may not have actually ended when inner gets called.

Indeed.

In [8]: def gen():
   ...:     for i in range(5):
   ...:         def inner():
   ...:             print i
   ...:         yield inner

In [9]: g = gen()

In [10]: outer = g.next()

In [11]: outer()
0

In [12]: g.next()

In [13]: outer()
1

Kent


More information about the Tutor mailing list