Class Variable Access and Assignment

Steve Holden steve at holdenweb.com
Thu Nov 3 07:56:10 EST 2005


Paul Rubin wrote:
> Steve Holden <steve at holdenweb.com> writes:
> 
>>>class A:
>>>  a = 1
>>>b = A()
>>>b.a += 2
>>>print b.a
>>>print A.a
>>>Which results in
>>>3
>>>1
>>>
>>
>>I don't suppose you'd care to enlighten us on what you'd regard as the
>>superior outcome?
> 
> 
>     class A:
>       a = []
>     b = A()
>     b.append(3)
>     print b.a
>     print a.a
> 
> Compare and contrast.

append() guarantees to modify a mutable object in place. Augmented 
assignment operations don't,but are "normally" equivalent to

   name = name operator value

In the former case exactly such semantics are implemented. I still don;t 
see anyone suggesting a better outcome for the augmented assignment.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list