On Nov 20, 2:03 pm, Peter Otten <__pete...@web.de> wrote: > Maxim Mercury wrote: > > here is the definintion of htmlelement > > > class HTMLElement: > > tag=None > > attrs={} > > data='' > > childs=[] > > > 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 ? > > In > > class A: > 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: > > class A: > def __init__(self): > self.some_list = [] > > Note that this holds for every attribute, but you usually see it only for > mutables like lists or dicts because in > > class A: > x = "yadda" > y = [] > a = A() > print a.x # yadda > a.x = 42 > print a.x # 42 > del a.x > print a.x # can you guess what happens? > > the class attribute is shadowed by the instance attribute whereas > > a.y.append(42) > > modifies the class attribute in place. > > # two more to check that you've understood the mechanism: > a.y += ["ham"] # ? > a.y = ["spam"] # ? > > Peter
Thanks a lot peter, that worked as i needed. Where can i find some good documentation which explains such behavior. -- http://mail.python.org/mailman/listinfo/python-list