[lxml-dev] is tostring() confusing?

Ian Bicking ianb at colorstudy.com
Wed Jun 13 20:42:41 CEST 2007


Stefan Behnel wrote:
> Hi Ian,
> 
> Ian Bicking wrote:
>> Having tostring() as a function and not a method seems a bit odd to me. 
>>   I know it's from ElementTree, but at least for HTML it's awkward -- 
>> using lxml.etree.tostring on HTML is almost certain to create bad 
>> output; the output won't be real XHTML (lacking namespaces and it'll 
>> probably be invalid), and it will parse quite badly as HTML (<script 
>> src="..."/> for instance will typically break the entire page in a browser).
>>
>> When I was first using ElementTree, I remember being a bit baffled by 
>> the lack of a serializing method.  I then found tostring and kind of 
>> forgot about it, but as I copy tostring methods around (e.g., 
>> lxml.html.tostring) it's starting to seem like a problem again.
> 
> I prefer keeping it the way it is in ET. After all, lxml.html is a module that
> is based on the existing ET API in lxml.etree. It would feel funny to have it
> one way in etree and another way in lhtml. It should be straight forward to
> change from one to the other depending on the problem to solve and that
> requires consistency.

I wouldn't propose adding it to just lxml.html, but it feels missing in 
all contexts.  That is, it seems like tostring would be a better method 
(on all kinds of elements) than a function.

   Ian


More information about the lxml-dev mailing list