简体   繁体   中英

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

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?

Replace the call:

super(ConstParser, self).__init__(defaults)

with:

SafeConfigParser.__init__(self, defaults)

and it works just fine without multiple Inheritance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM