在__init__中设置属性

时间:2018-09-26 14:35:01

标签: python class properties attributes

我想从B创建一个子类A,并使用__init__中的A,因为它在一个属性/属性上是相同的。

以下代码显示了我想做什么

class A:
    def __init__(self):
        self.a = 1
        self.b = 1
        self.c = 1

class B(A):
    def __init__(self):
        super().__init__()  # because I want 'a' and 'b', (but not 'c')

    @property
    def c(self):
        return 2

B()

跟踪:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-95c544214e48> in <module>()
     13         return 2
     14 
---> 15 B()

<ipython-input-9-95c544214e48> in __init__(self)
      7 class B(A):
      8     def __init__(self):
----> 9         super().__init__()  # because I want 'a' and 'b', (but not 'c')
     10 
     11     @property

<ipython-input-9-95c544214e48> in __init__(self)
      3         self.a = 1
      4         self.b = 1
----> 5         self.c = 1
      6 
      7 class B(A):

AttributeError: can't set attribute

我认为我可以通过解决这个问题

class B(A):
    def __init__(self):
        super().__init__()  # because I want 'a' and 'b', (but not 'c')
        self.c = property(lambda s: 2)

但是什么时候打电话:

>>> B().c
<property at 0x116f5d7c8>

该属性未评估。

如何在不从__init__手动复制A的情况下正确地做到这一点?

1 个答案:

答案 0 :(得分:1)

一种补救方法是也将c变成A中的一个属性;该属性仅返回(私有)成员self._c

class A:
    def __init__(self):
        self.a = 1
        self.b = 1
        self._c = 1

    @property
    def c(self):
        return self._c

class B(A):
    def __init__(self):
        super().__init__()  # because I want 'a' and 'b', (but not 'c')
        self._c = 2

    # is already inherited from A
    # @property
    # def c(self):
    #     return self._c

a = A()
b = B()
print(a.c)  # 1
print(b.c)  # 2

如果您无法更改A(并且假设您的属性的目的是使c为只读),则这是一个变体:c.setter将引发一个错误,如果self._c不是None

class A:
    def __init__(self):
        self.a = 1
        self.b = 1
        self.c = 1

class B(A):
    def __init__(self):
        self._c = None
        super().__init__()  # the setter for c will work as self._c = None
        self._c = 2         # now we set c to the new value 
                            # (bypassing the setter)

    @property
    def c(self):
        return self._c

    @c.setter
    def c(self, value):
        if self._c is not None:
            raise AttributeError
        self._c = value