ElementTree and clone element toot

Stefan Behnel stefan_ml at behnel.de
Mon Feb 9 08:20:35 CET 2009


Aahz wrote:
> In article <4986bfd7$0$9385$ba4acef3 at news.orange.fr>,
> m.banaouas <banaouas.medialog at wanadoo.fr> wrote:
>> Working with the ElementTree module, I looked for clone element
>> function but not found such tool:
> 
> Have you tried using copy.deepcopy()?

While being the most obvious solution, calling deepcopy() on an Element is
surprisingly slow in ElementTree.

http://codespeak.net/lxml/performance.html#deepcopy

It's actually multiple times faster to rebuild the tree manually through
the API than to let deepcopy() do it. Fredrik also has written a little
factory for mass reproduction of Element trees.

http://effbot.python-hosting.com/file/stuff/sandbox/elementlib/clone.py

Stefan



More information about the Python-list mailing list