tags:

views:

173

answers:

1

In a program that I'm writing, I wanted to make a ConfigParser that's read only so that it can safely be used globally. I didn't realize this, but apparently the SafeConfigParser is an old-style class, thus I had to subclass it like this:

class ConstParser(SafeConfigParser, object):
     """This is a implementation of the SafeConfigParser that can't
        write any values.  This is to ensure that it can only be filled
        once and won't get messy with multiple modules writing to it."""
    def __init__(self, files, defaults={}):
        super(ConstParser, self).__init__(defaults)
        self.read(files)
    def set(self, *args, **argd):
        raise NotImplementedError()
    def write(self, *args, **argd):
        raise NotImplementedError()
    def remove_option(self, *args, **argd):
        raise NotImplementedError()
    def remove_section(self, *args, **argd):
        raise NotImplementedError()

If I didn't use object as a mixin, the call to SafeConfigParser's __init__ method didn't work. Now, I'm sure that there are better ways to do what I want to do, but now I'm curious: is this ok to do in general?

I mean, I can't think of any reason why this would be a bad thing, but it still gives me bad feelings. Are they justified, or am I just being paranoid?

+2  A: 

Replace the call:

super(ConstParser, self).__init__(defaults)

with:

SafeConfigParser.__init__(self, defaults)

and it works just fine without multiple Inheritance.

Toni Ruža
But that's the old style, which he would like to get away from...
Cipher
There isn't really a reason to forcibly covert old-style classes form the standard library, they work just fine as they are. If there was a good reason for the ConfigParser to be a new-style class it would be by now...
Toni Ruža
Actually there is a REALLY good reason: newer language constructs(ie descriptors > properties..) dont work with old style classes...
Cipher
Yeah but is there a REALLY good reason to use those newer language constructs for ConfigParser? I'm all for using new-style classes but you don't need to get evangelical about it and go convert any and every old-style class you can find.
Toni Ruža
"There isn't really a reason to forcibly covert old-style classes form the standard library", i came back with a reason, you acknowledged that. what if he wants to inherit from that class and add a property to it??? PS: i am not "evangelical", the only reason i posted was to show you a reason...
Cipher
What I really meant was "there is rarely a good reason to forcibly convert...", I stand corrected.
Toni Ruža