Subclassing ConfigParser question

R

Roy H. Berger

If I want to subclass ConfigParser and changed the optionxform method
to not return things in lower case wouldn't I just need the following
code in my subclasss module?

from ConfigParser import *

class MyConfigParser(ConfigParser):
def __init__(self, defaults=None):
ConfigParser.__init__(self, defaults)

def optionxform (self, optionstr):
return optionstr

Or do I need to re-implement all of the methods of ConfigParser?
Sorry -- haven't done much subclassing with python.

Thanks in advance.

Roy
(e-mail address removed)
 
P

Peter Otten

Roy said:
If I want to subclass ConfigParser and changed the optionxform method
to not return things in lower case wouldn't I just need the following
code in my subclasss module?

from ConfigParser import *

class MyConfigParser(ConfigParser):
def __init__(self, defaults=None):
ConfigParser.__init__(self, defaults)

def optionxform (self, optionstr):
return optionstr

Or do I need to re-implement all of the methods of ConfigParser?
Sorry -- haven't done much subclassing with python.

You need to reimplement only methods with changed behaviour - that's the
whole idea of inheritance. In the above example even the constructor is
superfluous:

import ConfigParser # import * is *bad*

class MyConfigParser(ConfigParser.ConfigParser):
def optionxform(self, optionstr):
return optionstr

would suffice. There is of course no guarantee that other methods of
ConfigParser make assumptions about optionxform() that your modified method
does not hold. These you will have to detect with a properly designed test
suite.

Peter
 
G

Gary Richardson

Roy H. Berger said:
If I want to subclass ConfigParser and changed the optionxform method
to not return things in lower case wouldn't I just need the following
code in my subclasss module?

from ConfigParser import *

class MyConfigParser(ConfigParser):
def __init__(self, defaults=None):
ConfigParser.__init__(self, defaults)

def optionxform (self, optionstr):
return optionstr

Or do I need to re-implement all of the methods of ConfigParser?
Sorry -- haven't done much subclassing with python.

Thanks in advance.

Roy
(e-mail address removed)

Can't you just write:

cp = ConfigParser.ConfigParser()
cp.optionxform = str

to accomplish the same thing? That seems to be what the documentation
implies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top