[Tutor] Subclassing a module's class.

Magnus Lyckå magnus at thinkware.se
Fri Jul 23 15:35:59 CEST 2004


At 11:11 2004-07-22 -0700, Conrad wrote:
>I'm trying to subclass a class (HelpFormatter) from a module (optparse),
>so that all calls will use my Helpformatter instead of
>optparse.HelpFormatter.Currently I'm trying it like this:
>
>import optparse
>
>class HelpFormatter(optparse.HelpFormatter):
>         def format_option(self, option):
>                 print "Hello, World!"
>
>parser = optparse.OptionParser()
>print parser.print_help
>
>print_help() in optparse calls the class HelpFormatter. Can anyone
>explain to me why it doesnt use my HelpFormatter?


You need to tell the OptionParser to use it!

parser = optparse.OptionParser(formatter=HelpFormatter)

I suggest that you call your class something less generic
than just HelpFormatter though.


--
Magnus Lycka (It's really Lyckå), magnus at thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language 



More information about the Tutor mailing list