In Python 3.6, converting an xml `xml.etree.ElementTree.Element` to a string is done using `xml.etree.ElementTree.tostring()`. ``` from xml.etree import ElementTree xml = ElementTree.Element('Person', Name='John') print(ElementTree.tostring(xml, encoding='unicode', method='xml') # Output: ``` I would like to propose adding a `tostring()` function to the `Element` class, so that `ElementTree.tostring(xml)` could be replaced with the more intuitive `xml.tostring()`. ``` from xml.etree import ElementTree xml = ElementTree.Element('Person', Name='John') print(xml.tostring(encoding='unicode', method='xml')) # Output: ``` Benefits: - Doesn't require importing `xml.etree.ElementTree` - Allows writing more concise code - Makes `tostring` part of the `Element` class - Maintains backwards compatibility
Alternatively, the most intuitive solution would be to give `Element` an explicit `__str__` method. The current behavior of `str(Element)` is to return the object's location in memory. ``` from xml.etree import ElementTree xml = ElementTree.Element('Person', Name='John') print(str(xml)) # Output: <Element 'Person' at 0x028575D0> ``` Unfortunately, changing this behavior could cause issues with backwards compatibility.
Sorry, but you are proposing an API extension here that provides no benefits but duplicates existing functionality in a less versatile place. This is not going to happen. The second proposal (str(xml)) is actually not very helpful as it does not allow any kind of configuration, so it breaks backwards compatibility without benefit. Also not going to happen.