根据V1和V2分类变量V3。如何在子类中定义V3

2024-09-30 02:16:49 发布

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

Parent类由多个其他类继承

class Parent(object):

    V_1 = set()
    V_2 = set()

    ALL_V_ELEMENTS = V_1 | V_2

class Child1(Parent):
    V_1 = {1, }
    V_2 = {4, 7, 10}

class Child2(Parent):
    V_1 = {'a', 'b'}
    V_2 = {'a', 'c'}

V_1V_2在每个子级中都是不同的(而且它们在类创建后不会改变)

使用下面的代码,我得到了ALL_V_ELEMENTS的相同值:

print(Parent.ALL_V_ELEMENTS)  # prints: set()
print(Child1.ALL_V_ELEMENTS)  # prints: set()
print(Child2.ALL_V_ELEMENTS)  # prints: set()

这是我不想要的我需要的是这个

print(Parent.ALL_V_ELEMENTS)  # prints: set()
print(Child1.ALL_V_ELEMENTS)  # prints: {1, 10, 4, 7}
print(Child2.ALL_V_ELEMENTS)  # prints: {'a', 'c', 'b'}

为了实现我的目标,我可以如下定义类:

class Child1(Parent):
    V_1 = {1, }
    V_2 = {4, 7, 10}
    ALL_V_ELEMENTS = V_1 | V_2

class Child2(Parent):
    V_1 = {'a', 'b'}
    V_2 = {'a', 'c'}
    ALL_V_ELEMENTS = V_1 | V_2

然而,在Parent的每个子级上复制粘贴ALL_V_ELEMENTS = V_1 | V_2似乎不是一个好主意

另一种选择是以不同的方式定义Parent

class Parent(object):

    V_1 = set()
    V_2 = set()

    def __init__(self):
        self.ALL_V_ELEMENTS = self.V_1 | self.V_2

这将对每个冗余实例执行|操作


有没有更好的方法来实现我的目标


Tags: 代码self目标定义objectelementsallprints
1条回答
网友
1楼 · 发布于 2024-09-30 02:16:49

您可以将其定义为属性:

class Parent(object):

    V_1 = set()
    V_2 = set()

    @property
    def ALL_V_ELEMENTS(self):
       return V_1 | V_2

但是,每次都会重新计算集合。让__init__创建集合意味着将为每个实例创建集合

您可以在元类中计算集合,因此只有在生成类对象时才会生成集合:

class AllVMeta(type):
    def __new__(typ, name, bases, attrs):
        cls = super(AllVMeta, typ).__new__(typ, name, bases, attrs)
        cls.ALL_V_ELEMENTS = cls.V_1 | cls.V_2
        return cls

这个元类向任何子类添加一个ALL_V_ELEMENTS并集;像这样使用它:

class Parent(object, metaclass=AllVMeta):
    V_1 = set()
    V_2 = set()

class Child1(Parent):
    V_1 = {1, }
    V_2 = {4, 7, 10}

class Child2(Parent):
    V_1 = {'a', 'b'}
    V_2 = {'a', 'c'}

演示:

>>> class AllVMeta(type):
...     def __new__(typ, name, bases, attrs):
...         cls = super(AllVMeta, typ).__new__(typ, name, bases, attrs)
...         cls.ALL_V_ELEMENTS = cls.V_1 | cls.V_2
...         return cls
...
>>> class Parent(object, metaclass=AllVMeta):
...     V_1 = set()
...     V_2 = set()
...
>>> class Child1(Parent):
...     V_1 = {1, }
...     V_2 = {4, 7, 10}
...
>>> class Child2(Parent):
...     V_1 = {'a', 'b'}
...     V_2 = {'a', 'c'}
...
>>> Child1.ALL_V_ELEMENTS
{1, 10, 4, 7}
>>> Child2.ALL_V_ELEMENTS
{'a', 'c', 'b'}

相关问题 更多 >

    热门问题