[Tutor] What on earth is happening here ???

Alan Gauld alan.gauld at btinternet.com
Wed Jul 25 22:02:47 CEST 2012


On 25/07/12 16:11, Lipska TheCat wrote:

> from xml.dom.minidom import getDOMImplementation

I'd probably start by saying that I suspect elemtTree will be easier to 
use than minidom, but if you must....


> class NpDOMDocumentFactory:
>      # make these private "by convention"
>      __DOMImplementation = getDOMImplementation()
>      __defaultNamespaceUri = "http://nuldomain.com/"
>      __defaultQualifiedName = "root"
>      __defaultDoctype = __DOMImplementation.createDocumentType("HTML",

Its not C++ or Java, Python doesn't usually need "private" definitions...


>      def getDOMDocument(self):
>      def getDOMDocument(self, namespaceUri=__defaultNamespaceUri):
>      def getDOMDocument(self, namespaceUri=__defaultNamespaceUri, qualifiedName=__defaultQualifiedName):
 >      def getDOMDocument(self,
                           namespaceUri=__defaultNamespaceUri,
                           qualifiedName=__defaultQualifiedName,
                           docType=__defaultDoctype):

These definitions all define the same object. There is no function 
overloading in Python. You can often fake it with suitable use of 
default arguments. This looks likely here.

If you can't use defaulted values you can write helper functions and 
call the main method with a tuple of arguments and switch on the length 
of tuple.

Or you can use the argument expansion mechanism to pass unlimited 
numbers of arguments, or a set of keyword arguments. Lots of options.

> ...I just need to understand why the 3 arg method is being called
 > instead of the 0 arg method as expected

Because in Python the function name alone is the identifier and so the 
last version defined is the version used.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list