N
neoone
Hi, I reported a bug to the bugtracker (issue 1443), but it was
rejected with the comment:
"Go ask on c.l.py why this is not a bug"
After decrypting c.l.py to the name of this group, I'll do as I was
told so nicely, because I really think it is a misconcept, and cost me
two days because I couldn't believe it.
So here's the problem:
The Initialization of member variables with lists leads to strange
behavior.
The member variable is common to each instance of a class, if the
datatype is a list-Object (and presumable any other PyObject)
Example:
#-------------------------------------------------------
class Proof:
a=[]
b=[]
def __init__(self):
print self.a, self.b, self
self.a.append("STICKYARRAY")
self.b=["NONSTICKY ASSIGN"]
if __name__ == "__main__":
p1=Proof()
p2=Proof()
#-------------------------------------------------------
The execution of this results in:
So the initialized list a is the same in both instances, but they are
completely different objects.
Well, I can think of the coders problem, that when creating the
object, always the same copy of the list object is being taken which
has once been defined, but it doesn't makes sense, because they are
not really static like C++, because as soon as you reassign them, they
are lost.
Bye,
Neo
rejected with the comment:
"Go ask on c.l.py why this is not a bug"
After decrypting c.l.py to the name of this group, I'll do as I was
told so nicely, because I really think it is a misconcept, and cost me
two days because I couldn't believe it.
So here's the problem:
The Initialization of member variables with lists leads to strange
behavior.
The member variable is common to each instance of a class, if the
datatype is a list-Object (and presumable any other PyObject)
Example:
#-------------------------------------------------------
class Proof:
a=[]
b=[]
def __init__(self):
print self.a, self.b, self
self.a.append("STICKYARRAY")
self.b=["NONSTICKY ASSIGN"]
if __name__ == "__main__":
p1=Proof()
p2=Proof()
#-------------------------------------------------------
The execution of this results in:
[] [] <__main__.Proof instance at 0x00BA7120>
['STICKYARRAY'] [] <__main__.Proof instance at 0x00BA7148>
So the initialized list a is the same in both instances, but they are
completely different objects.
Well, I can think of the coders problem, that when creating the
object, always the same copy of the list object is being taken which
has once been defined, but it doesn't makes sense, because they are
not really static like C++, because as soon as you reassign them, they
are lost.
Bye,
Neo