Static variable vs Class variable

Diez B. Roggisch deets at nospam.web.de
Wed Oct 10 02:23:02 EDT 2007


>> Yes, it is.
> 
> I'm afraid not.
> 
> As I admitted in my reply to Marc, I overstated my case by saying that L 
> isn't rebound at all. Of course it is rebound, but to itself.
> 
> However, it is not true that += "always leads to a rebinding of a to the 
> result of the operation +". The + operator for lists creates a new list. 
> += for lists does an in-place modification:


It still is true.

a += b

rebinds a. Period. Which is the _essential_ thing in my post, because 
this rebinding semantics are what confused the OP.




>>>> L = []
>>>> M = L
>>>> L += [1]
>>>> M
> [1]
> 
> Compare with:
> 
>>>> L = []
>>>> M = L
>>>> L = L + [1]
>>>> M
> []
> 
> You said:
> 
> "I presume you got confused by the somewhat arbitrary difference between 
> __add__ and __iadd__ that somehow suggest there is an in-place-
> modification going on in case of mutables but as the following snippet 
> shows - that's not the case: ..."

Admittedly, I miss _one_ word here: necessarily before the "an".

> That's an explicit denial that in-place modification takes place, and 
> that's *way* off the mark. I was concentrating so hard on showing in-
> place modification that I glossed over the "return self" part.

And I was concetrating so hard on the rebinding-part, I glossed over the 
in-place-modification part.

Diez



More information about the Python-list mailing list