I'm hoping someone can explain this behavior, it gave me quite a headache trying to figure out what was going wrong with my code.
Say we set up some simple etree elements like so
from copy import deepcopy
from lxml import etree
elem1=etree.Element('e1')
elem2=etree.Element('e2')
If I do this,
elem_copy=deepcopy(elem1).append(elem2)
elem_copy
comes out as NoneType
However, if I just break out the steps like this
elem_copy=deepcopy(elem1)
elem_copy.append(elem2)
I get the expected behavior with a new element in elem_copy
and elem2
as a child element.
Can anyone explain why this is?
In the first case
elem_copy=deepcopy(elem1).append(elem2)
the result of append
i.e. None
is getting assigned back to elem_copy
In the second case
elem_copy=deepcopy(elem1)
elem_copy.append(elem2)
the result of append
is not getting assigned back to elem_copy
. As a result of this, elem_copy
has the element returned by deepcopy
with the second element appended to it.
Hope that helps.