重载Python类属性的优雅方法

时间:2011-07-29 08:37:23

标签: python properties overloading

考虑一个基类:

class A(object):
   def __init__(self, x):
      self._x = x

   def get_x(self):
      #...
      return self._x

   def set_x(self, x):
      #...
      self._x = x

   x = property(get_x, set_x)

和派生类:

class B(A):
   def set_x(self, x):
      #...
      self._x = x**2

   x = property(A.get_x, set_x)

是否有一种优雅的方法可以在课程set_x()中重载B,而无需重新声明它和属性x?谢谢。

2 个答案:

答案 0 :(得分:3)

添加一个额外的间接层(即使用钩子):

class A(object):
    def __init__(self, x):
        self._x = x

    # Using a _get_hook is not strictly necessary for your problem...
    def _get_hook(self):
        return self._x
    def get_x(self):
        return self._get_hook()

    # By delegating `set_x` behavior to `_set_hook`, you make it possible
    # to override `_set_hook` behavior in subclass B.
    def _set_hook(self, x):
        self._x=x
    def set_x(self, x):
        self._set_hook(x)

    x = property(get_x, set_x)

class B(A):
    def _set_hook(self, x):
        print('got here!')
        self._x = x**2

b=B(5)
b.x=10
# got here!
print(b.x)
# 100

对于现代版本的Python,您还可以使用@property装饰器:

class A(object):

   @property
   def x(self):
      return self._get_hook()

   @x.setter
   def x(self, x):
      self._set_hook(x)

答案 1 :(得分:2)

试试这个:

 class A(object):
    def __init__(self):
        self._x = 0

    def get_x(self):
        #...
        return self._x

    def set_x(self, x):
        #...
        self._x = x

    x = property(get_x, lambda self,x : self.set_x(x))

class B(A):
    def set_x(self, x):
        #...
        self._x = x**2

lambda给出的额外间接将使set_x函数虚拟化。