ElementTree and clone element toot

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Feb 3 02:17:19 CET 2009


En Mon, 02 Feb 2009 14:21:29 -0200, m.banaouas  
<banaouas.medialog at wanadoo.fr> escribió:

> My python version is 2.4.4
>
> def SubElement(parent, tag, attrib={}, **extra):
>
> Can you tell me how does "parent" issue could be solved by SubElement ?

Simply because you *have* to pass a parent to the function...

> I'm looking for how to determine element parent just by asking element  
> it self.

There isn't a way -- and there are good reasons. One is memory management  
(it would create reference cycles, and they aren't efficiently removed).  
Another one is that it prevents an element to be shared between containers.

> My goal is to clone an element and attach it to a specified parent or to  
> parant of original element to clone.

Then pass the desired parent as the "parent" argument.

En Mon, 02 Feb 2009 13:52:59 -0200, Gerard Flanagan <grflanagan at gmail.com>
escribió:

> Not sure what SubElement gains you, in the context of the above function?

Just because Element and SubElement are documented as the recommended way  
to create Element instances. Also, I was under the impression that  
makeelement did not make a copy of its "attrib" argument, so it would end  
being shared by other instances too -- but it isn't the case actually.

-- 
Gabriel Genellina




More information about the Python-list mailing list