Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.

python class instantiation

Yametazamwa mara 3
Ruka hadi kwenye ujumbe wa kwanza ambao haujasomwa

Éric Daigneault lists

hayajasomwa,
23 Okt 2006, 16:56:0123/10/2006
kwa pytho...@python.org
Got a question for you all...

I noticed a behaviour in python class creation that is strange, to say
the least.

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

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")
instance.stringData = "Changed"

Now if I do

secondInstance = A()

it will come with the listData containing the SomeString appended to the
instance...

this is clearly not normal

Especially that the stringData of Second instance contains the
"Whatever" text. If the behaviour was at least consistant... but it is
not...

Am I coing nuts or is this by desing, if so it is very misleading...
The two instances are sharing the same list, but not the same string
... I did not declare the list to be static in any way.... Why does it
behave like this ?

Éric

Fredrik Lundh

hayajasomwa,
23 Okt 2006, 17:02:3223/10/2006
kwa pytho...@python.org
É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>

Éric Daigneault

hayajasomwa,
23 Okt 2006, 17:24:2823/10/2006
kwa pytho...@python.org
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

Chetan

hayajasomwa,
23 Okt 2006, 18:35:1223/10/2006
kwa
This is not what I get :
Here is the code and the output for 2.5

class A(object):
stringData = "Whatever"
listData = []

inst = A()

print inst.stringData
print inst.listData
print

inst.listData.append("SomeString")
inst.stringData = "Changed"

inst2 = A()

print inst2.stringData
print inst2.listData
print

inst.listData.append("NewThing")
inst.stringData = "NewChanged"

print inst.stringData
print inst.listData
print inst.stringData
print inst.listData
print

-----
Whatever
[]

Whatever
['SomeString']

NewChanged
['SomeString', 'NewThing']
NewChanged
['SomeString', 'NewThing']

Isn't this what you got?

-Chetan

Gabriel Genellina

hayajasomwa,
23 Okt 2006, 19:15:3123/10/2006
kwa dai...@gmail.com,pytho...@python.org
At Monday 23/10/2006 17:56, É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.

See
<http://zephyrfalcon.org/labs/python_pitfalls.html> specially items 4 and 5.
And <http://www.effbot.org/zone/python-objects.htm>


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar

Paul McGuire

hayajasomwa,
23 Okt 2006, 19:22:4323/10/2006
kwa
"Éric Daigneault" <dai...@gmail.com> wrote in message
news:mailman.1017.1161640...@python.org...

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

+1 QOTW


Ujumbe 0 mpya