Creating a document tree before or after adding subitems

I use lxml and Python to write XML files. I was wondering what the accepted practice is: first create a document tree and then add auxiliary elements or add auxiliary elements and create a tree later? I know that this is unlikely to have any meaning in relation to the output, but I was interested to know what is the accepted norm in this from the point of view of coding.

Code example:

page = etree.Element('root')
#first create the tree
doc = etree.ElementTree(page) 
#add the subelements
headElt = etree.SubElement(page, 'head')

Or that:

page = etree.Element('root')
headElt = etree.SubElement(page, 'head')
#create the tree in the end
doc = etree.ElementTree(page) 
+3
source share
1 answer

, , , , . , , . , , , , , , , , , ,

+1

Source: https://habr.com/ru/post/1745254/


All Articles