classification
Title: Class members not properly initialized if declared outside of function
Type: behavior Stage: committed/rejected
Components: Versions: Python 2.5
process
Status: closed Resolution: invalid
Dependencies: Superseder:
Assigned To: Nosy List: r.david.murray, vladc6 (2)
Priority: low Keywords

Created on 2009-11-02 21:55 by vladc6, last changed 2009-11-02 22:18 by r.david.murray.

Files
File name Uploaded Description Edit Remove
class_member_init_inconsistency.py vladc6, 2009-11-02 21:55 Sample program demonstrating inconsistency in member initialization
Messages (2)
msg94851 - (view) Author: Vlad (vladc6) Date: 2009-11-02 21:55
If a class member is declared outside of a function, it's creation is
_not_ repeated with every new instance of the class. Instead, the same
member is created during the first instance and then shared by all the
subsequent instances of that class. This is wrong, because a new member
should be created for every instance.

I have attached a sample code contrasting the incorrect behavior (on
top) with the correct behavior (on bottom). Python behaves correctly if
the member is declared in __init__ (that is, Python initializes a new
member for every instance).

The output of "print b.greet" and "print d.greet" should be the same and
should read "['Howdy']".
msg94854 - (view) Author: R. David Murray (r.david.murray) Date: 2009-11-02 22:18
This is the way python works.  For help on using python please post to
python-list or python-tutor.
History
Date User Action Args
2009-11-02 22:18:54r.david.murraysetstatus: open -> closed


nosy: + r.david.murray
messages: + msg94854
priority: low
resolution: invalid
stage: committed/rejected
2009-11-02 21:55:35vladc6create