"no variable or argument declarations are necessary."

Duncan Booth duncan.booth at invalid.invalid
Thu Oct 6 04:40:21 EDT 2005


Antoon Pardon wrote:

>> from xml.dom import *
>>
>> def do_add(x, y):
>>      return '%s://%s' % (x, y)
>>
>> def do_something(node):
>>      if node.namespace == XML_NAMESPACE:
>>          return do_add('http://', node.namespace)
>>      elif node.namespace == ...
>>      ...
>>
> 
> IMO your variable are already mostly declared. The x and y in
> the do_add is kind of a declarartion for the parameters x and
> y. 

I think you missed his point, though I'm not surprised since unless you 
are familiar with the internals of the xml package it isn't obvious just 
how complex this situation is.

The value XML_NAMESPACE was imported from xml.dom, but the xml package is 
kind of weird. XML_NAMESPACE defined both in xml.dom and in the 
_xmlplus.dom package. The _xmlplus package is conditionally imported by the 
xml package, and completely replaces it, but only if _xmlplus is present 
and at least version 0.8.4 (older versions are ignored).

This is precisely the kind of flexibility which gives Python a lot of its 
power, but it means that you cannot tell without running the code which 
package actually provides xml.dom.

Of course, I would expect that if you enforced strict variable declarations 
you would also disallow 'from x import *', but you still cannot tell until 
runtime whether an particular module will supply a particular variable, not 
what type it is.



More information about the Python-list mailing list