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

2012-07-25 Thread Jerry Hill
On Wed, Jul 25, 2012 at 11:11 AM, Lipska TheCat
lipskathe...@yahoo.co.uk wrote:
 def getDOMDocument(self):

This defines the method getDOMDocument().

 def getDOMDocument(self, namespaceUri=__defaultNamespaceUri):

This defines the method getDOMDocument(), replacing the previous definition.

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

This defines the method getDOMDocument() a third time, replacing the
second definition.


 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

 I'm not asking for a critique of my code and I realise that something spooky
 is going on with the doctype stuff I just need to understand why the 3 arg 
 method is being called instead of the

In python there can only ever be one object bound to a particular
name.  You cannot have three different functions that share the same
name, but have different signatures.  Instead, define a single method
and use default arguments to supply the defaults if someone doesn't
pass in a value.

-- 
Jerry
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-07-25 Thread Prasad, Ramit
 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


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

2012-07-25 Thread Alan Gauld

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/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-07-25 Thread Steven D'Aprano

Alan Gauld wrote:


 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.


Another alternative is the generics implementation from pkgutil:

from pkgutil import simplegeneric

See the source code for details -- simplegeneric is public but undocumented. 
From time to time people make noise about documenting it and moving it into 
its own module, but this is volunteer-driven and it hasn't happened yet.




--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor