lambda closure question

jfj jfj at freemail.gr
Mon Feb 21 17:36:29 EST 2005


Antoon Pardon wrote:
> Op 2005-02-19, jfj schreef <jfj at freemail.gr>:
> 
>>once foo() returns there is no way to modify 'x'!
>>It becomes a kind of constant.
> 
> 
> In this particular case yes. But not in general, what about
> this:
> 
> 
>>>>def F():
> 
> ...   l = []
> ...   def pop():
> ...     return l.pop()
> ...   def push(e):
> ...     l.append(e)
> ...   return pop, push
> ... 
> 

Others will point this out, but if I'm fast enough...

This does not change the object referenced by l.
It calls methods of it and because it is mutable the containts
of the list are modified.
'l' is a list at address, 0xXXXX and that can never change once
F() has returned.


jfj



More information about the Python-list mailing list