constructin trees in python
__peter__ at web.de
Sat Nov 20 10:03:49 CET 2010
Maxim Mercury wrote:
> here is the definintion of htmlelement
> class HTMLElement:
> the issue is the though new elements are pushed in the stack (1),
> whenever i append the child to the stack top all other elements in the
> stack is getting affected, i assume the same reference is used but is
> there a way to overcome this ?
some_list = 
defines a class attribute shared by all instances of A. To turn some_list
into an instance attribute move the definition into the initializer:
self.some_list = 
Note that this holds for every attribute, but you usually see it only for
mutables like lists or dicts because in
x = "yadda"
y = 
a = A()
print a.x # yadda
a.x = 42
print a.x # 42
print a.x # can you guess what happens?
the class attribute is shadowed by the instance attribute whereas
modifies the class attribute in place.
# two more to check that you've understood the mechanism:
a.y += ["ham"] # ?
a.y = ["spam"] # ?
More information about the Python-list