理解@property装饰器和继承[重复]

2024-05-09

这里是 Python 3,以防万一它很重要。

我试图正确理解如何实现继承@property使用,我已经搜索了 StackOverflow 并阅读了大约 20 个类似的问题,但无济于事,因为他们试图解决的问题略有不同。这是我用于测试的代码:

class Example:
    def __init__(self):
        self.__data = None

    @property
    def data(self):
        return self.__data

    @data.setter
    def data(self, data):
        self.__data = data


class Example2(Example):
    def __init__(self):
        super().__init__()

    @property
    def data(self):
        return super().data  # Works!

    @data.setter
    def data(self, data):
        data = '2' + data
        #Example.data = data   # Works, but I want to avoid using the parent name explicitly
        #super().data = data  # Raises AttributeError: 'super' object has no attribute 'data'
        #super().data.fset(self, data) # Raises AttributeError: 'NoneType' object has no attribute 'fset'
        #super(self.__class__, self.__class__).data = data  # Raises AttributeError: 'super' object has no attribute 'data'
        super(self.__class__, self.__class__).data.fset(self, data)  # Works!


a = Example2()
a.data = 'element a'
print(a.data)

我不明白的是为什么super().data工作于Example2getter,但不在 setter 中。我的意思是,为什么在 setter 中需要类绑定方法,但在 getter 中需要实例绑定方法?

任何人都可以向我指出解释或解释为什么我得到AttributeError在我正在测试的五个不同调用中的三个中?

是的,我知道,我可以使用Example.data在 setter 中,但在 getter 中不需要这样做,并且 a) 如果可能的话,我宁愿不显式使用父类名称 b) 我不理解 getter 和 setter 之间的不对称性。


你应该这样做:

class Example:
    def __init__(self):
        self._data = None

    @property
    def data(self):
        return self._data

    @data.setter
    def data(self, data):
        self._data = data


class Example2(Example):
    def __init__(self):
        super().__init__()

    @Example.data.setter
    def data(self, data):
        data = '2' + data
        self._data = data


    a = Example2()
    a.data = 'element a'
    print(a.data)

您收到属性错误,因为该类没有数据属性,而实例有它。

如果你想覆盖@property,只需这样做:

class Example:
def __init__(self):
    self._data = None

    @property
    def data(self):
        return self._data

    @data.setter
    def data(self, data):
        self._data = data


class Example2(Example):
    def __init__(self):
    super().__init__()

    @property
    def data(self):
        return self._data

    @data.setter
    def data(self, data):
        data = '2' + data
        self._data = data
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

理解@property装饰器和继承[重复] 的相关文章

随机推荐