473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1752
É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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jules Dubois | last post by:
I'm want to create a superclass with nothing but attributes and properties. Some of the subclasses will do nothing but provide values for the attributes. (I'd also like to make sure (1) that the...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
14
by: David MacQuigg | last post by:
I am starting a new thread so we can avoid some of the non-productive argument following my earlier post "What is good about Prothon". At Mr. Hahn's request, I will avoid using the name "Prothon"...
114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
86
by: Matthias Kaeppler | last post by:
Hi, sorry for my ignorance, but after reading the Python tutorial on python.org, I'm sort of, well surprised about the lack of OOP capabilities in python. Honestly, I don't even see the point at...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
8
by: Ole Nielsby | last post by:
I want to create (with new) and delete a forward declared class. (I'll call them Zorgs here - the real-life Zorks are platform-dependent objects (mutexes, timestamps etc.) used by a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.