class Element():
def __init__(self, tag, childs = [], attributes = {}):
self.childs = childs
self.tag = tag
self.attrib = attributes
def get_childs(self):
return self.childs
def add_Child(self, child):
self.childs.append(child)
class Html():
def __init__(self):
self.tag1 = Element("head")
self.tag2 = Element("body")
tag0 = Element("html", [self.tag1, self.tag2])
def get_body(self):
return self.tag2
def get_head(self):
return self.tag1
def main():
html_object = Html()
print html_object.get_body().get_childs()
print html_object.get_head().get_childs()
print "---------------"
html_object.get_body().add_Child("new_child_added_to_body_ELEMENT")
print "---------------"
print html_object.get_body().get_childs()
print html_object.get_head().get_childs()
if __name__ == "__main__":
main()
when executing the above lines of code i get the following output:
[]
[]
---------------
---------------
['new_child_added_to_body_ELEMENT']
['new_child_added_to_body_ELEMENT']
while i wanted to insert the 'new_child_added_to_body_ELEMENT' only to the self.childs list of self.tag2 (the "body" Element"), what i end up getting is that the line is also added to the self.childs list of self.tag1 (the "head" Element) that is also declared inside of the Html class init.
there's obviously something about python classes that i'm missing so an explanation about what i'm doing wrong will be really appreciated.