[XML-SIG] Re: [4suite] dom question

Alexandre Fayolle Alexandre.Fayolle@logilab.fr
Wed, 17 Oct 2001 09:05:58 +0200 (CEST)


On Tue, 16 Oct 2001, markus jais wrote:

> hello
> thanks for your question.
> 
> one more:
> you said, that the attribute childNodes is in FtNode, line 103
> but there is:
> 
> def _get_childNodes(self):
>         return self.__dict__['__childNodes']
> 
> the method has an underscore at the beginning
> and the attribute has two "__"
> but there is not "childNodes"
> 
> I am not a python wizard, so maybe this is a python idiom
> I do not understand

The _get_XXX is part of the IDL to python mapping (DOM is defined as a set
of IDL interfaces). Not all Python DOM implementations provide this
mapping. The _get_childNodes method is called through the __getattr__
method (this is a python method that is called on a class when the
interpreter was not able to find a field, attribute or method, see
http://www.python.org/doc/current/ref/attribute-access.html#l2h-115). 

The __getattr__ method of FtNode (line 68) looks up a dictionnary called
_readComputedAttrs (defined line 425), which says that the method to read
childNodes is _get_childNodes, and then calls the method and returns the
result. 

Now, the question is, why do we do that ? It's because we want to protect
access to the childNode attribute, especially write access, while
preserving the attribute notation. So this python idiom is used to provide
a read-only attribute. There is also a _writeComputedAttrs dictionnary
dictionnary, which enable write access to some attributes, through methods
that will enable checking of the arguments. 

Does this answer your question?

Alexandre Fayolle
-- 
LOGILAB, Paris (France).
http://www.logilab.com   http://www.logilab.fr  http://www.logilab.org
Narval, the first software agent available as free software (GPL).