使用多重继承将oldstyle类变成newstyle类安全吗?

2024-04-25 13:49:57 发布

您现在位置:Python中文网/ 问答频道 /正文

在我正在编写的程序中,我想制作一个只读的ConfigParser,以便它可以安全地在全局范围内使用。我没有意识到这一点,但是显然SafeConfigParser是一个旧的样式类,因此我不得不将其分为以下类:

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()

如果我没有将对象用作mixin,那么对SafeConfigParser的^{cd1>}方法的调用将无法工作。现在,我确信有更好的方法来做我想做的事情,但现在我很好奇:一般来说,这样做可以吗?

我是说,我想不出为什么这会是件坏事,但它仍然给我带来了不好的感觉。他们是正当的,还是我只是偏执?


Tags: toselfthatisdefargsitthis