[XML-SIG] Ugh! Why are DOM access methods spelled with a leading '_'?

Paul Prescod paul@prescod.net
Mon, 26 Jun 2000 11:35:33 -0700


Jim Fulton wrote:
> 
> 
> > In 4DOM, we are actually moving away from __getattr__ (for speed).
> 
> IMO, this is strong evidence that the Python DOM should
> *not* use attributes for implementing the DOM/IDL attributes

Direct attribute access is MUCH, MUCH faster than a method call, whethr
through getattr or not. Under the current implementation we have the
option of caching attributes for performance. An all method design would
take that away. Minidom uses direct attributes for 95% of what it does.
I think I used one getter method to lazily evaluate attributes.

> I'd like it to be as easy as possible for various objects to implement
> the DOM. (See for example StructuredTextNG.) I'd hate to make implementers
> go through the pain and performance hit of getattr or dictate an implementation
> (like caching attributes or otherwise directly storing them, creating
> memory leaks).

I guess the question is who do we cater to? Heretofore it has been DOM
users first, DOM implementors second. I don't think that we should turn
that around based on the argument that all Python objects will have a
DOM interface soon.

To me, this looks like Python:

a=b.childNodes[0].attributes["abc"]

and this looks like Java:

a=b.getChildNodes()[0].getAttributes()["abc"]

The second grates on me as having interface enforced because of
implementation limitations. (which is what all of this griping about
getattr being slow boils down to...isn't it better to fix that problem
once, for everyone than to work around it a hundred times?) It also
drives me crazy that the latter always invokes a method call even when
it is stored underneath as a simple attribute.

Surely there is some imaginative way to make life easier for your
implementors using base classes. For instance, wouldn't it be nice for
you to automatically set up the attributes list based on Python
attributes? Something like:

def __getattr__( self, name ):
	if name=="attributes":
		keys=self.__dict__.keys()
		values=map( str, self.__dict__.values()
		return JimsAttributeList( keys, values )

Encourage them to subclass from you but add some value that they
wouldn't get otherwise.

-- 
 Paul Prescod - Not encumbered by corporate consensus
The "war on drugs" began as a rhetorical flourish used by Richard 
Nixon...  But as the Reagan, Bush and Clinton administrations 
poured billions of dollars into fighting drugs, the slogan slipped 
the reins of metaphor to become just a plain old war - with an 
army (DEA), an enemy (profiled minorities, the poor, the cities), 
a budget ($17.8 billion), and a shibboleth (the children).
	- "This is your bill of rights...on drugs", Harper's, Dec 1999