[OT] Re: lambda closure question

Steven Bethard steven.bethard at gmail.com
Mon Feb 21 13:50:23 EST 2005


Antoon Pardon wrote:
>>>>def F():
> ...   l = []
> ...   def pop():
> ...     return l.pop()
> ...   def push(e):
> ...     l.append(e)
> ...   return pop, push
> ... 

Just a side note to point out that another way of writing this is:

py> def F():
...     l = []
...     return l.pop, l.append
...

You'll get the same behavior:

py> pop, push = F()
py> push(1)
py> pop()
1
py> push(2)
py> push(3)
py> pop()
3
py> pop()
2

Hooray for bound methods! ;)

STeVe



More information about the Python-list mailing list