471,896 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,896 software developers and data experts.

python class instantiation

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
Oct 23 '06 #1
1 1703
√Čric Daigneault wrote:
>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
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
Oct 23 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jules Dubois | last post: by
699 posts views Thread by mike420 | last post: by
14 posts views Thread by David MacQuigg | last post: by
114 posts views Thread by Maurice LING | last post: by
7 posts views Thread by Drew McCormack | last post: by
86 posts views Thread by Matthias Kaeppler | last post: by
23 posts views Thread by mark.moore | last post: by
8 posts views Thread by Ole Nielsby | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.