How best to return a class method?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Oct 11 13:58:37 CEST 2004


Roy Smith wrote:
> I've got a method which returns another method:
> 
> class domPrinter:
>     def getNodePrinter (self, nodeType):
> 	[...]
>         # return domPrinter.printUnknown
> 	return self.__class__.printUnknown
> 
> I originally wrote it as the commented-out version, but didn't like
> the idea that I've got to repeat the class name.  The version using
> self.__class__ solves that problem, but it's uglier and more
> complicated.
> 
> Any opinions on the best way to do this?

I don't really notice anything uglier or more complicated here (2nd 
solution), and that's how I'd do it, unless I have some compelling 
reason to prevent use of a redifined version of printUnknown in a child 
class (for now, I never found a compelling enough reason...)

Bruno




More information about the Python-list mailing list