ElementTree should parse string and file in the same way

Stefan Behnel stefan.behnel-n05pAM at web.de
Thu Jan 3 03:08:36 EST 2008


Hi,

Chris Mellon wrote:
> On that note, I really don't like APIs that take either a file name or
> a file object - I can open my own files, thanks.

... and HTTP URLs, and FTP URLs. In lxml, there is a performance difference
between passing an open file (which is read in Python space using the read()
method) and passing a file name or URL, which is passed on to libxml2 (and
thus doesn't require the GIL at parse time). That's only one reason why I like
APIs that allow me to pass anything that points to a file - be it an open file
object, a local file path or a URL - and they just Do The Right Thing with it.

I find that totally pythonic.


> open(fname) is even shorter than StringIO(somedata).

It doesn't serve the same purpose, though.


> My take on the API decision in question was always that a file is
> inherently an XML *document*, while a string is inherently an XML
> *fragment*.

Not inherently, no. I know some people who do web processing with an XML
document coming in as a string (from an HTTP request) and a result XML
document going out as a string. I don't think that's an uncommon use case.

Stefan



More information about the Python-list mailing list