Issue7254
Created on 2009-11-02 21:55 by vladc6, last changed 2009-11-02 22:18 by r.david.murray.
| File name |
Uploaded |
Description |
Edit |
Remove |
|
class_member_init_inconsistency.py
|
vladc6,
2009-11-02 21:55
|
Sample program demonstrating inconsistency in member initialization |
|
|
|
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.
|
|
| Date |
User |
Action |
Args |
| 2009-11-02 22:18:54 | r.david.murray | set | status: open -> closed
nosy:
+ r.david.murray messages:
+ msg94854 priority: low resolution: invalid stage: committed/rejected |
| 2009-11-02 21:55:35 | vladc6 | create | |
|