A
ahart
I'm pretty new to python and am trying to write a fairly small
application to learn more about the language. I'm noticing some
unexpected behavior in using lists in some classes to hold child
objects. Here is some abbreviated code to help me explain.
####################################
class Item(object)
__text = ""
def __get_text(self):
return self.__text
def __set_text(self, value):
self.__text = value
text = property(fget=__get_text, fset=__set_text)
def __init__(self, text=""):
self.__text = text
#...some other methods here...
class Parent(object):
__items = []
def __get_items(self):
return self.__items
items = property(fget=__get_items)
def addItem(self, item):
"""Adds an Item object to the internal list."""
self.__items.append(item)
def __init__(self, items=[]):
if(len(items)>0):
for item in items:
self.addItem(item)
def __str__(self):
s = "<parent>"
for item in self.__items:
s += "<item>%s</item>" % item.text
s += "</parent>"
#...some other methods here...
if(__name__=="__main__"):
i1 = Item("one")
i2 = Item("two")
i3 = Item("three")
p1 = Parent([i1, i2])
p2 = Parent([i3])
print str(p1)
####################################
When I run this script, I expect to see the following string printed:
"<parent><item>one</item><item>two</item></parent>"
Instead, I see the following:
"<parent><item>one</item><item>two</item><item>three</item></parent>"
Apparently, the p1 instance somehow thinks that the i3 instance is in
its list. The i3 instance should instead be in the list for p2. By the
way, when I call the __str__() method of p2, I get the same results as
when I do it for p1. The list appears to be acting as if it were a
static member - which it is not.
I do have some @classmethod methods in these classes in my complete
script. Would that confuse the interpreter into thinking that other
members are also static?
I can't seem to find any information on this problem. Does anyone have
any ideas?
Thank you.
application to learn more about the language. I'm noticing some
unexpected behavior in using lists in some classes to hold child
objects. Here is some abbreviated code to help me explain.
####################################
class Item(object)
__text = ""
def __get_text(self):
return self.__text
def __set_text(self, value):
self.__text = value
text = property(fget=__get_text, fset=__set_text)
def __init__(self, text=""):
self.__text = text
#...some other methods here...
class Parent(object):
__items = []
def __get_items(self):
return self.__items
items = property(fget=__get_items)
def addItem(self, item):
"""Adds an Item object to the internal list."""
self.__items.append(item)
def __init__(self, items=[]):
if(len(items)>0):
for item in items:
self.addItem(item)
def __str__(self):
s = "<parent>"
for item in self.__items:
s += "<item>%s</item>" % item.text
s += "</parent>"
#...some other methods here...
if(__name__=="__main__"):
i1 = Item("one")
i2 = Item("two")
i3 = Item("three")
p1 = Parent([i1, i2])
p2 = Parent([i3])
print str(p1)
####################################
When I run this script, I expect to see the following string printed:
"<parent><item>one</item><item>two</item></parent>"
Instead, I see the following:
"<parent><item>one</item><item>two</item><item>three</item></parent>"
Apparently, the p1 instance somehow thinks that the i3 instance is in
its list. The i3 instance should instead be in the list for p2. By the
way, when I call the __str__() method of p2, I get the same results as
when I do it for p1. The list appears to be acting as if it were a
static member - which it is not.
I do have some @classmethod methods in these classes in my complete
script. Would that confuse the interpreter into thinking that other
members are also static?
I can't seem to find any information on this problem. Does anyone have
any ideas?
Thank you.