
Hi all
I noticed a behaviour in lxml that surprised me, so I thought I would mention it.
Versions - python 3.3.2, lxml 3.2.1.
Consider the following - standard python slicing -
a = [1, 2, 3, 4] b = [5, 6, 7, 8] a[:0] = b[0:] a
[5, 6, 7, 8, 1, 2, 3, 4]
b
[5, 6, 7, 8]
This is what I expected.
Then look at this - lxml slicing -
from lxml import etree a = etree.fromstring('<a1><a1.1/><a1.2/></a1>') b = etree.fromstring('<b1><b1.1/><b1.2/></b1>') a[:0] = b[0:] etree.tostring(a)
b'<a1><b1.1/><b1.2/><a1.1/><a1.2/></a1>'
etree.tostring(b)
b'<b1/>'
The slicing worked, but it removed the contents of b.
Is this intentional?
Frank Millman

Frank Millman, 26.08.2013 08:24:
I noticed a behaviour in lxml that surprised me, so I thought I would mention it.
Versions - python 3.3.2, lxml 3.2.1.
Consider the following - standard python slicing -
a = [1, 2, 3, 4] b = [5, 6, 7, 8] a[:0] = b[0:] a
[5, 6, 7, 8, 1, 2, 3, 4]
b
[5, 6, 7, 8]
This is what I expected.
Then look at this - lxml slicing -
from lxml import etree a = etree.fromstring('<a1><a1.1/><a1.2/></a1>') b = etree.fromstring('<b1><b1.1/><b1.2/></b1>') a[:0] = b[0:] etree.tostring(a)
b'<a1><b1.1/><b1.2/><a1.1/><a1.2/></a1>'
etree.tostring(b)
b'<b1/>'
The slicing worked, but it removed the contents of b.
Is this intentional?
Yes, it's intentional and documented in a couple of places, e.g. here
http://lxml.de/tutorial.html#elements-are-lists
Stefan

----- Original Message ----- From: "Stefan Behnel" stefan_ml@behnel.de To: "lxml mailing list" lxml@lxml.de Sent: Monday, August 26, 2013 8:45 AM Subject: Re: [lxml] Surprising slicing behaviour
Frank Millman, 26.08.2013 08:24:
I noticed a behaviour in lxml that surprised me, so I thought I would mention it.
[...]
The slicing worked, but it removed the contents of b.
Is this intentional?
Yes, it's intentional and documented in a couple of places, e.g. here
Got it. Thanks, Stefan.
Frank
participants (2)
-
Frank Millman
-
Stefan Behnel