用同名的@属性覆盖基础class属性
Overwrite base class attribute with @property of the same name
我正在尝试子class python class 并用@属性 函数覆盖常规属性。问题是我无法修改父 class,子 class 的 api 需要看起来与父 class 相同(但行为不同) . (所以我的问题与 this one 不同,其中父 class 也使用 @属性 方法来访问底层属性。)
最简单的例子是
# assume this class can't be overwritten
class Parent(object):
def __init__(self, a):
self.attr = a
# how do I make this work?
class Child(Parent):
def __init__(self, a):
super(Child, self).__init__(a)
# overwrite access to attr with a function
@property
def attr(self):
return super(Child, self).attr**2
c = Child(4)
print c.attr # should be 16
这会在调用父 init 方法时产生错误。
<ipython-input-15-356fb0400868> in __init__(self, a)
2 class Parent(object):
3 def __init__(self, a):
----> 4 self.attr = a
5
6 # how do I make this work?
AttributeError: can't set attribute
希望清楚我想做什么以及为什么。但是我想不通。
这可以通过添加 setter 方法轻松解决
class Child(Parent):
def __init__(self, a):
self._attr = None
super(Child, self).__init__(a)
# overwrite access to a with a function
@property
def attr(self):
return self._attr**2
@attr.setter
def attr(self, value):
self._attr = value
我正在尝试子class python class 并用@属性 函数覆盖常规属性。问题是我无法修改父 class,子 class 的 api 需要看起来与父 class 相同(但行为不同) . (所以我的问题与 this one 不同,其中父 class 也使用 @属性 方法来访问底层属性。)
最简单的例子是
# assume this class can't be overwritten
class Parent(object):
def __init__(self, a):
self.attr = a
# how do I make this work?
class Child(Parent):
def __init__(self, a):
super(Child, self).__init__(a)
# overwrite access to attr with a function
@property
def attr(self):
return super(Child, self).attr**2
c = Child(4)
print c.attr # should be 16
这会在调用父 init 方法时产生错误。
<ipython-input-15-356fb0400868> in __init__(self, a)
2 class Parent(object):
3 def __init__(self, a):
----> 4 self.attr = a
5
6 # how do I make this work?
AttributeError: can't set attribute
希望清楚我想做什么以及为什么。但是我想不通。
这可以通过添加 setter 方法轻松解决
class Child(Parent):
def __init__(self, a):
self._attr = None
super(Child, self).__init__(a)
# overwrite access to a with a function
@property
def attr(self):
return self._attr**2
@attr.setter
def attr(self, value):
self._attr = value