Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to make an old-style class into a new-style class using Multiple Inheritance?

Tags:

python

oop

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?

like image 938
Jason Baker Avatar asked Nov 03 '08 18:11

Jason Baker


1 Answers

Replace the call:

super(ConstParser, self).__init__(defaults)

with:

SafeConfigParser.__init__(self, defaults)

and it works just fine without multiple Inheritance.

like image 115
Toni Ruža Avatar answered Oct 08 '22 12:10

Toni Ruža