[lxml-dev] Checking whether a node is a comment/element
Stefan Behnel
stefan_ml at behnel.de
Sat Jul 19 09:48:47 CEST 2008
Hi,
Geoffrey Sneddon wrote:
> What's the best way to check whether a given node is a comment or an
> element? For the former, I'm currently using isinstance(node,
> etree._Comment), which is rather obviously sub-optimal.
You can do
node.tag is etree.Comment
to check if it's a comment, and
isinstance(node.tag, basestring) # Py2.x
or
isinstance(node.tag, str) # Py3
to see if it's a normal Element.
This may be sub-optimally obvious, but I find it better than checking for
underscored classes.
Stefan
More information about the lxml-dev
mailing list