lambda closure question

Antoon Pardon apardon at forel.vub.ac.be
Mon Feb 21 07:41:20 EST 2005


Op 2005-02-21, jfj schreef <jfj at freemail.gr>:
> 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.

I'm sorry but in my understanding of english mutating an object
is changing or modifying it. Yes it is stil the same list but
that list was not unmodified.

But I'll get back at what seems you actually wanted to say:
That there is no way to rebind 'x' or in my case 'l' and
with that I have to agree although I personnaly find that
a lack in python

-- 
Antoon Pardon



More information about the Python-list mailing list