> I have a class
> The point of this class is to provide an uncluttered interface
> to Document Creation
> 
> 
> from xml.dom.minidom import getDOMImplementation
> 
> class NpDOMDocumentFactory:
> 
>     """A DOM Document Factory convenience class"""
> 
> 
> 
>     # make these private "by convention"
>     __DOMImplementation = getDOMImplementation()
>     __defaultNamespaceUri = "http://nuldomain.com/";
>     __defaultQualifiedName = "root"
>     __defaultDoctype = __DOMImplementation.createDocumentType("HTML",
>                                                             "-//W3C//DTD HTML
> 4.01//EN",

Trying to make things "private" is a throwback to Java. In Python
the tendency is to just leave everything "public". And "private" is with
one underbar/underscore.

> 
> "http://www.w3.org/TR/html4/strict.dtd";)
>     #params required for the createDocument method on DOMImplementation
>     #DOMImplementation.createDocument(namespaceUri, qualifiedName, doctype)
> 
>     #get a default document with the root element initialised to <root>
>     def getDOMDocument(self):
>         print("no args")
>         return self.__DOMImplementation.createDocument(None, "root", None)
> 
>     #allow a user to specify the namespaceUri node name
>     def getDOMDocument(self, namespaceUri=__defaultNamespaceUri):
>         return self.__DOMImplementation.createDocument(namespaceUri, "root",
> None)
>
> 
>     #allow a user to specify the namespaceUri and a qualifiedName
>     def getDOMDocument(self, namespaceUri=__defaultNamespaceUri,
> qualifiedName=__defaultQualifiedName):
>         return self.__DOMImplementation.createDocument(namespaceUri,
> qualifiedName, None)
> 
> 
>     #allow a user to specify the namespaceUri, a qualifiedName and a doctype
>     def getDOMDocument(self, namespaceUri=__defaultNamespaceUri,
> qualifiedName=__defaultQualifiedName, docType=__defaultDoctype):
>         print("3 args")
>         return self.__DOMImplementation.createDocument(namespaceUri,
> qualifiedName, docType)
> 
> 
> 
> #end NpDOMDocumentFactory
> 
> factory = NpDOMDocumentFactory()
> 
> print(factory.getDOMDocument().toxml())
> 
> when I pass this to python I get the following
> 
> lipska@ubuntu:~/python/dev/classes/com/nuldomain/xml$ python3.2
> NpDOMDocumentFactory.py
> 3 args
> <?xml version="1.0" ?><!DOCTYPE HTML  PUBLIC '-//W3C//DTD HTML 4.01//EN'
> 'http://www.w3.org/TR/html4/strict.dtd'><root/>
> 
> I have absolutely no idea why a method that requires three arguments is being
> called when I intend the "no args" method to be called
>

Python does not have polymorphism in the same manner as Java.
Instead of creating different methods with a different number of
arguments, you create one method with "optional" arguments, i.e.
"keyword" arguments. 

def function( something=None ):
        print(something)

This creates a function where the argument 'something' is optional.
If nothing is passed in, then something will default to None. If 
something is passed in as either a positional or keyword argument--
func('test') or func(something='test')--then the value of something
will be whatever is passed in.

Now applying that to your method I see you have three different
return statements.

return self.__DOMImplementation.createDocument(namespaceUri, 
    qualifiedName, docType)
return self.__DOMImplementation.createDocument(namespaceUri,
    qualifiedName, None)
return self.__DOMImplementation.createDocument(namespaceUri, 
    "root", None)


So I will use the method definition.

def getDOMDocument(self, namespaceUri=__defaultNamespaceUri,
 qualifiedName=__defaultQualifiedName, docType=__defaultDoctype):
         return self.__DOMImplementation.createDocument(namespaceUri,
             qualifiedName, docType)


Now in order to get the same returns as above I will call 
it with the following options.

self.getDOMDocument(namespace_url, qualified_name, doc_type )
self.getDOMDocument(namespace_url, qualified_name, docType=None )
self.getDOMDocument(namespace_url, docType=None, qualifiedName="root" )


The last line can also be written entirely as positional arguments.
self.getDOMDocument(namespace_url, "root", None )

Or if you want to use all the default arguments
self.getDOMDocument()



Ramit

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to