4

首先-如果这是重复的,请接受我的道歉-我感觉我以前看过某种类似的讨论,但我真的找不到。

我的问题是关于 Python 中的对象组合,它应该看起来像是从复合类的每个次要中继承的。用例是多个对象实例共享一个共同的属性核心及其值(而不仅仅是一个共同的结构,这将是一个经典的继承案例)。

我可以用一个简单的属性来做到这一点,即让每个类都有一个名为“shared_attributes”的属性,它本身就是一个存储所有值的类:

class CoreClass(object):
    def __init__(self):
        self.attr = 'asdf'


class CompClass1(object):
    def __init__(self, core):
        self.core_attr = core


class CompClass2(object):
    def __init__(self, core):
        self.core_attr = core

但这需要我通过属性访问每个共享属性class.core_attr,这是我不想要的(出于多种原因,其中一个原因是这将需要大量重写大部分代码)。

因此,我想使用依赖于 Python 内置__getattr__对象方法的复合模式,例如:

class TestClass1(object):
    def __init__(self):
        self.attr1 = 1

    def func_a(self):
        return 'a'


class CompClassBase(object):
    def __init__(self, test_class):
        self.comp_obj = test_class

    def __getattr__(self, item):
        return getattr(self.comp_obj, item)


class CompClass1(CompClassBase):
    def __init__(self, test_class):
        CompClassBase.__init__(self, test_class)
        self.attr2 = 13

    def func_b(self):
        return '1b'


class CompClass2(CompClassBase):
    def __init__(self, test_class):
        CompClassBase.__init__(self, test_class)
        self.attr2 = 23

    def func_b(self):
        return '2b'


if __name__ == '__main__':
    tc = TestClass1()
    cc1 = CompClass1(test_class=tc)
    cc2 = CompClass2(test_class=tc)
    print cc1.attr1
    print cc1.attr2
    print cc1.func_a()
    print cc1.func_b()
    print cc2.attr1
    print cc2.attr2
    print cc2.func_a()
    print cc2.func_b()

它应该打印以下内容:

1
13
a
1b
1
23
a
2b

这种模式完全符合我的需求,但它有一些东西想让我确定它......

编辑:(回应一些评论)这个模式必须共享共享类中的所有属性(给定以前的对象):

cc1.attr1 = 'this is a test'
cc2.attr1  # must now be 'this is a test' as well!

第二次编辑:我已经使用这种模式几个星期了,效果很好。但是,我仍然希望进行一些讨论,因为我想从现在开始将此模式包含在我的标准工具包中:-)

所以现在我给你的问题很简单:这是一个好习惯吗?这种特殊的 Python 模式有什么缺点吗?我应该注意这里的一些危险吗?

4

0 回答 0