Nested function scope problem

Gerhard Fiedler gelists at gmail.com
Fri Jul 28 10:41:30 EDT 2006


On 2006-07-28 04:07:20, Bruno Desthuilliers wrote:

> Gerhard Fiedler a écrit :
>> Isn't being on the LHS (of an assignment) the only way to (re)bind a
>> variable?
> 
> <pedantic>
> s/variable/name/
> </pedantic>

Ok, I missed this one :)

>> Are there situations where binding happens without an explicit
>> assignment with the variable name being on the LHS?
> 
> Assignment aside, binding occurs:
> 
> 1/ on evaluation of import, class and def statement
> 2/ on function call for the function's arguments
> 3/ as a result of a list comp or for loop:

Sounds intuitive enough.

>> Can the object referenced by a variable name get changed without the
>> variable name appearing on the LHS?
> 
>  >>> a = []
>  >>> id(a)
> 1078043820
>  >>> def change(l):
> ...     l.append(42)
> ...
>  >>> change(a)
>  >>> a
> [42]
>  >>> id(a)
> 1078043820
>  >>>
> 
> First assignment aside, the *name* 'a' is never on the LHS of an 
> assignment, yet the object referenced by name 'a' got 'changed' : it was 
> initially empty, after call to change() it contains an int.
> 
> Yes, I know, this is not what you meant (and no, name 'a' has not been 
> rebound) - but that's really what you asked :  the object referenced by 
> name 'a' got changed without name 'a' being on the LHS !-) 

What I meant here is whether the object can be a different one than before
(the object gets changed like a shirt before dinner, not like the color of
crabs during cooking), without it being caused by being on the LHS of an
assignment (or any of the other possibilities you listed above). That was
more an ambiguity in the language...

> I think your intented question was more like : "can a name be rebound 
> without that name being on the LHS of an assignment", [...]

Yes. 

> and it's addressed above

I'll have to experiment a bit and come up with an example of what I'm
thinking of. But it seems that what I was thinking of doesn't make sense in
the light of my better understanding of binding :)

Thanks for your help. Binding seems to be one of the more crucial concepts
to master before understanding how Python works. One of the first things I
wondered (this is slightly related) is whether it wouldn't be really good
to make the difference between mutable and immutable objects more obvious. 

Gerhard




More information about the Python-list mailing list