Static variable vs Class variable
Diez B. Roggisch
deets at nospam.web.de
Wed Oct 10 02:35:01 EDT 2007
Diez B. Roggisch schrieb:
>>> 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.
Which I just came around to show in a somewhat enhanced example I could
have used the first time:
class Foo(object):
a = []
@classmethod
def increment(cls):
print cls
cls.a += [1]
class Bar(Foo):
pass
Foo.increment()
Bar.increment()
print Foo.a
print Bar.a
Bar.a = []
print Foo.a
print Bar.a
192:~/projects/SoundCloud/ViewAnimationTest deets$ python /tmp/test.py
<class '__main__.Foo'>
<class '__main__.Bar'>
[1, 1]
[1, 1]
[1, 1]
[]
Which makes the rebinding-part of __iadd__ pretty much an issue, don't
you think?
Diez
More information about the Python-list
mailing list