[issue7254] Class members not properly initialized if declared outside of function

Vlad report at bugs.python.org
Mon Nov 2 22:55:36 CET 2009


New submission from Vlad <vladc6 at yahoo.com>:

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']".

----------
files: class_member_init_inconsistency.py
messages: 94851
nosy: vladc6
severity: normal
status: open
title: Class members not properly initialized if declared outside of function
type: behavior
versions: Python 2.5
Added file: http://bugs.python.org/file15253/class_member_init_inconsistency.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7254>
_______________________________________


More information about the Python-bugs-list mailing list