Class Variable Access and Assignment

Steve Holden steve at holdenweb.com
Thu Nov 3 13:20:06 CET 2005


Antoon Pardon wrote:
> Op 2005-11-03, Steven D'Aprano schreef <steve at REMOVETHIScyber.com.au>:
> 
> 
>>>There are two possible fixes, either by prohibiting instance variables
>>>with the same name as class variables, which would allow any reference
>>>to an instance of the class assign/read the value of the variable. Or
>>>to only allow class variables to be accessed via the class name itself.
>>
>>There is also a third fix: understand Python's OO model, especially
>>inheritance, so that normal behaviour no longer surprises you.
> 
> 
> No matter wat the OO model is, I don't think the following code
> exhibits sane behaviour:
> 
> 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?

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