Class properties and object properties

Jerry Hill malaclypse2 at gmail.com
Mon Oct 6 10:04:59 EDT 2008


On Mon, Oct 6, 2008 at 9:38 AM, SuperZE <allansuperze at gmail.com> wrote:
> Interesting, but that does not explain the difference in the behavior
> of myList and myInt
>
> Both were class-level variables, as far as I can see, and therefor a
> and b should also share it

They did share it, until you assigned an instance variable in b, which
shadowed the class variable.  Example:

>>> class Test1:
	myInt = 4

	
>>> a = Test1()
>>> b = Test1()
>>> a.myInt
4
>>> b.myInt
4
>>> Test1.myInt
4
>>> b.myInt = 3
>>> a.myInt
4
>>> b.myInt
3
>>> Test1.myInt
4

As soon as you bound the name b.myInt to a new value, it created an
instance variable.  That hides the value of Test1.myInt.

-- 
Jerry



More information about the Python-list mailing list