471,305 Members | 1,278 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

Subclassing ConfigParser question

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
ro*****@att.net
Jul 18 '05 #1
2 4116
Roy H. Berger wrote:
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
Jul 18 '05 #2

"Roy H. Berger" <ro*****@att.net> wrote in message
news:b6***********************@posting.google.com. ..
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
ro*****@att.net


Can't you just write:

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

to accomplish the same thing? That seems to be what the documentation
implies.
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Greg Krohn | last post: by
1 post views Thread by Martin Maney | last post: by
11 posts views Thread by Manlio Perillo | last post: by
2 posts views Thread by rzed | last post: by
10 posts views Thread by Terry Carroll | last post: by
1 post views Thread by pipehappy | last post: by
3 posts views Thread by tony.ha | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.