Static variable vs Class variable

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Tue Oct 9 18:53:46 EDT 2007


On Tue, 09 Oct 2007 22:27:47 +0200, Diez B. Roggisch wrote:

> Steven D'Aprano schrieb:
>> On Tue, 09 Oct 2007 19:23:37 +0200, Diez B. Roggisch wrote:
>> 
>>> Your believes aside, this is simply wrong. The statement
>>>
>>> a += x
>>>
>>> always leads to a rebinding of a to the result of the operation +.
>> 
>> Not true.
> 
> 
> 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:

>>> 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: ..."

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.



-- 
Steven.



More information about the Python-list mailing list