python class instantiation

Fredrik Lundh fredrik at pythonware.com
Mon Oct 23 17:02:32 EDT 2006


Éric Daigneault lists wrote:

> When creating a class with data members but no __init__ method.  Python 
> deals differently with data members that are muatable and immutables.

no, it doesn't.  it's your code that deals with them in different ways, 
not Python.

> Ex:
> class A(object):
>     stringData = "Whatever"
>     listData = []
> 
> instance = A()
> 
> Will have data members instantiated as expected (instance.stringData == 
> "Whatever" and instance.listData == [])
> 
> instance.listData.append("SomeString")

here, you call a method on the class object.  this method modifies the 
object.

> instance.stringData = "Changed"

here, you use assignment to *add* a new attribute to the instance.

the class attribute is still there, but it's shadowed by an instance 
attribute with the same name.

</F>




More information about the Python-list mailing list