Newbie Question: can this snippet be optimized?

Jp Calderone exarkun at
Wed Jun 25 19:20:26 CEST 2003

On Wed, Jun 25, 2003 at 03:08:00PM +0000, Hedr at wrote:
> Hi Pythonians,
> The underlying snippet is copied from which is in the XML
> package which I am currently using. Cloning nodes seem to take a
> considerable amount of time, hence I went looking in the code whether
> optimizations could be made. The following snippet contains a for
> loop:
>     def cloneNode(self, deep):
>         clone = Node.cloneNode(self, deep)
>         clone._attrs = {}
>         clone._attrsNS = {}
>         for attr in self._attrs.values():
>             node = attr.cloneNode(1)
>             clone._attrs[] = node
>             clone._attrsNS[(node.namespaceURI, node.localName)] = node
>             node.ownerElement = clone
>         return clone

  Using "itervalues" instead of "values" may produce some minor improvement.
values() creates a new list object containing references to all the values
in the dictionary, while itervalues() only creates a very light-weight


Seduced, shaggy Samson snored.
She scissored short.  Sorely shorn,
Soon shackled slave, Samson sighed,
Silently scheming,
Sightlessly seeking
Some savage, spectacular suicide.
                -- Stanislaw Lem, "Cyberiad"

More information about the Python-list mailing list