[lxml-dev] Checking whether a node is a comment/element
Hi, 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. -- Geoffrey Sneddon <http://gsnedders.com/>
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
On 19 Jul 2008, at 08:48, Stefan Behnel wrote:
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.
Yeah, seems better, though slightly bizarre. Thanks. -- Geoffrey Sneddon
How can I change a JS element to see if it is a node or an empty variable? https://tunnel-rush.io
participants (6)
-
andersenlabvb@gmail.com
-
chickenplusone@gmail.com
-
ferrasonandreaa@gmail.com
-
Geoffrey Sneddon
-
marianpearson340@gmail.com
-
Stefan Behnel