python class instantiation

Éric Daigneault daigno at gmail.com
Mon Oct 23 17:24:28 EDT 2006


Fredrik Lundh wrote:
> É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.
>   
I see.. so the all the attributes I declared above are part of the 
class... not the instance, kinna like instance attributes being 
overshadowed with local method attributes...

>> 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>
>   
yep... got it...

guess the Java-C++ views on things kinna creapt on me there...

Thanks for clearing that up :-)

It is humbling to see how simple yet powerfull python`s view on things 
is....

Éric






More information about the Python-list mailing list