This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Class members not properly initialized if declared outside of function
Type: behavior Stage: resolved
Components: Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: r.david.murray, vladc6
Priority: low Keywords:

Created on 2009-11-02 21:55 by vladc6, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
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) * (Python committer) 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
2022-04-11 14:56:54adminsetgithub: 51503
2009-11-02 22:18:54r.david.murraysetstatus: open -> closed
priority: low


nosy: + r.david.murray
messages: + msg94854
resolution: not a bug
stage: resolved
2009-11-02 21:55:35vladc6create