Subclassing ConfigParser question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roy H. Berger

    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
    roybrew@att.net
  • Peter Otten

    #2
    Re: Subclassing ConfigParser question

    Roy H. Berger wrote:
    [color=blue]
    > 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.[/color]

    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.Co nfigParser):
    def optionxform(sel f, 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

    Comment

    • Gary Richardson

      #3
      Re: Subclassing ConfigParser question


      "Roy H. Berger" <roybrew@att.ne t> wrote in message
      news:b62ff6e.03 11250836.7460f2 @posting.google .com...[color=blue]
      > 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
      > roybrew@att.net[/color]

      Can't you just write:

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

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


      Comment

      Working...