[XML-SIG] Specializing DOM exceptions

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Tue, 2 Jan 2001 23:34:55 +0100


> > Seriously, after a quick survey of my code, the only place I
> > import Node is in order to get at the constants.

> Yup, I noticed this in 4Suite code

Actually, when editing 4DOM, I found that a number of places uses Node
as a base class, so you still need to import the module.

> Is there a difference in performance with:
> "if some_node.nodeType == some_node.ELEMENT_NODE :" ?

Yes, but it should not matter much. If you have an inheritance depth
of 4 (xml.dom.Node, xml.dom.FtNode.Node, xml.dom.Element.Element,
something that derives from Element), then you get 5 dictionary
lookups to find self.ELEMENT_NODE (for the instance, and for each of
the bases).

For Node.ELEMENT_NODE, you get only two (one to find Node, one to find
ELEMENT_NODE); three if you look in FtNode.Node, four if you write
xml.dom.Node.ELEMENT_NODE.

Since dictionary lookups were tuned to be one of the most efficient
operations in Python, and since it is so easy to get many dictionary
lookups in other places, that really shouldn't matter much.

So what counts would be clarity, I have to admit that I find
Node.ELEMENT_NODE clearer than self.ELEMENT_NODE (although either is
obvious if you know the DOM).

Regards,
Martin