如果基类的属性在派生类中被覆盖,如何调用该属性?

发布于 2024-07-25 05:51:01 字数 747 浏览 7 评论 0原文

我正在将我的一些类从广泛使用 getter 和 setter 改为更Python 化地使用属性。

但现在我陷入了困境,因为我以前的一些 getter 或 setter 会调用基类的相应方法,然后执行其他操作。 但是如何通过属性来实现这一点呢? 如何调用父类中的属性getter或setter?

当然,仅调用属性本身就会产生无限递归。

class Foo(object):

    @property
    def bar(self):
        return 5

    @bar.setter
    def bar(self, a):
        print a

class FooBar(Foo):

    @property
    def bar(self):
        # return the same value
        # as in the base class
        return self.bar # --> recursion!

    @bar.setter
    def bar(self, c):
        # perform the same action
        # as in the base class
        self.bar = c    # --> recursion!
        # then do something else
        print 'something else'

fb = FooBar()
fb.bar = 7

I'm changing some classes of mine from an extensive use of getters and setters to a more pythonic use of properties.

But now I'm stuck because some of my previous getters or setters would call the corresponding method of the base class, and then perform something else. But how can this be accomplished with properties? How to call the property getter or setter in the parent class?

Of course just calling the attribute itself gives infinite recursion.

class Foo(object):

    @property
    def bar(self):
        return 5

    @bar.setter
    def bar(self, a):
        print a

class FooBar(Foo):

    @property
    def bar(self):
        # return the same value
        # as in the base class
        return self.bar # --> recursion!

    @bar.setter
    def bar(self, c):
        # perform the same action
        # as in the base class
        self.bar = c    # --> recursion!
        # then do something else
        print 'something else'

fb = FooBar()
fb.bar = 7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

老旧海报 2024-08-01 05:51:01

您可能认为您可以调用由属性调用的基类函数:

class FooBar(Foo):

    @property
    def bar(self):
        # return the same value
        # as in the base class
        return Foo.bar(self)

虽然这是最明显的尝试,但我认为 - 它不起作用,因为 bar 是一个属性,而不是可调用的。

但是属性只是一个对象,有一个 getter 方法来查找相应的属性:

class FooBar(Foo):

    @property
    def bar(self):
        # return the same value
        # as in the base class
        return Foo.bar.fget(self)

You might think you could call the base class function which is called by property:

class FooBar(Foo):

    @property
    def bar(self):
        # return the same value
        # as in the base class
        return Foo.bar(self)

Though this is the most obvious thing to try I think - it does not work because bar is a property, not a callable.

But a property is just an object, with a getter method to find the corresponding attribute:

class FooBar(Foo):

    @property
    def bar(self):
        # return the same value
        # as in the base class
        return Foo.bar.fget(self)
唐婉 2024-08-01 05:51:01

super() 应该解决这个问题:

return super().bar

在 Python 2.x 中,您需要使用更详细的语法:

return super(FooBar, self).bar

super() should do the trick:

return super().bar

In Python 2.x you need to use the more verbose syntax:

return super(FooBar, self).bar
揽清风入怀 2024-08-01 05:51:01

还有一种使用 super 的替代方法,它不需要显式引用基类名称。

基类A:

class A(object):
    def __init__(self):
        self._prop = None

    @property
    def prop(self):
        return self._prop

    @prop.setter
    def prop(self, value):
        self._prop = value

class B(A):
    # we want to extend prop here
    pass

在B中,访问父类的属性getter A:

正如其他人已经回答的那样,它是:

super(B, self).prop

或者在Python 3中:

super().prop

这返回属性的getter返回的值,而不是getter本身,但它足以延长吸气剂。

在 B 中,访问父类 A 的属性 setter:

迄今为止我见过的最好的建议如下:

A.prop.fset(self, value)

我相信这个更好:

super(B, self.__class__).prop.fset(self, value)

在这个例子中,两个选项是等效的,但使用 super 的优点是独立于B 的基类。 如果 B 从同时扩展该属性的 C 类继承,则无需更新 B 的代码。

B 扩展 A 属性的完整代码:

class B(A):
    @property
    def prop(self):
        value = super(B, self).prop
        # do something with / modify value here
        return value

    @prop.setter
    def prop(self, value):
        # do something with / modify value here
        super(B, self.__class__).prop.fset(self, value)

一个警告:

除非您的属性没有 setter,否则您必须在 B 中定义 setter 和 getter,即使您只更改其中之一的行为他们。

There is an alternative using super that does not require to explicitly reference the base class name.

Base class A:

class A(object):
    def __init__(self):
        self._prop = None

    @property
    def prop(self):
        return self._prop

    @prop.setter
    def prop(self, value):
        self._prop = value

class B(A):
    # we want to extend prop here
    pass

In B, accessing the property getter of the parent class A:

As others have already answered, it's:

super(B, self).prop

Or in Python 3:

super().prop

This returns the value returned by the getter of the property, not the getter itself but it's sufficient to extend the getter.

In B, accessing the property setter of the parent class A:

The best recommendation I've seen so far is the following:

A.prop.fset(self, value)

I believe this one is better:

super(B, self.__class__).prop.fset(self, value)

In this example both options are equivalent but using super has the advantage of being independent from the base classes of B. If B were to inherit from a C class also extending the property, you would not have to update B's code.

Full code of B extending A's property:

class B(A):
    @property
    def prop(self):
        value = super(B, self).prop
        # do something with / modify value here
        return value

    @prop.setter
    def prop(self, value):
        # do something with / modify value here
        super(B, self.__class__).prop.fset(self, value)

One caveat:

Unless your property doesn't have a setter, you have to define both the setter and the getter in B even if you only change the behaviour of one of them.

变身佩奇 2024-08-01 05:51:01

Maxime 的回答的一些小改进:

  • 使用 __class__ 避免编写 B。 请注意,self.__class__self 的运行时类型,但 __class__ without self > 是封闭类定义的名称。 super()super(__class__, self) 的简写。
  • 使用 __set__ 而不是 fset。 后者特定于属性,但前者适用于所有类似属性的对象(描述符)。
class B(A):
    @property
    def prop(self):
        value = super().prop
        # do something with / modify value here
        return value

    @prop.setter
    def prop(self, value):
        # do something with / modify value here
        super(__class__, self.__class__).prop.__set__(self, value)

Some small improvements to Maxime's answer:

  • Using __class__ to avoid writing B. Note that self.__class__ is the runtime type of self, but __class__ without self is the name of the enclosing class definition. super() is a shorthand for super(__class__, self).
  • Using __set__ instead of fset. The latter is specific to propertys, but the former applies to all property-like objects (descriptors).
class B(A):
    @property
    def prop(self):
        value = super().prop
        # do something with / modify value here
        return value

    @prop.setter
    def prop(self, value):
        # do something with / modify value here
        super(__class__, self.__class__).prop.__set__(self, value)
漫漫岁月 2024-08-01 05:51:01

尝试一下

@property
def bar:
    return super(FooBar, self).bar

虽然我不确定python是否支持调用基类属性。 属性实际上是一个可调用对象,它使用指定的函数进行设置,然后替换类中的该名称。 这很容易意味着没有可用的超级功能。

不过,您始终可以切换语法以使用 property() 函数:

class Foo(object):

    def _getbar(self):
        return 5

    def _setbar(self, a):
        print a

    bar = property(_getbar, _setbar)

class FooBar(Foo):

    def _getbar(self):
        # return the same value
        # as in the base class
        return super(FooBar, self)._getbar()

    def bar(self, c):
        super(FooBar, self)._setbar(c)
        print "Something else"

    bar = property(_getbar, _setbar)

fb = FooBar()
fb.bar = 7

try

@property
def bar:
    return super(FooBar, self).bar

Although I'm not sure if python supports calling the base class property. A property is actually a callable object which is set up with the function specified and then replaces that name in the class. This could easily mean that there is no super function available.

You could always switch your syntax to use the property() function though:

class Foo(object):

    def _getbar(self):
        return 5

    def _setbar(self, a):
        print a

    bar = property(_getbar, _setbar)

class FooBar(Foo):

    def _getbar(self):
        # return the same value
        # as in the base class
        return super(FooBar, self)._getbar()

    def bar(self, c):
        super(FooBar, self)._setbar(c)
        print "Something else"

    bar = property(_getbar, _setbar)

fb = FooBar()
fb.bar = 7
我们只是彼此的过ke 2024-08-01 05:51:01

您可以使用以下模板:

class Parent():
    def __init__(self, value):
        self.__prop1 = value

    #getter
    @property
    def prop1(self):
        return self.__prop1

    #setter
    @prop1.setter
    def prop1(self, value):
        self.__prop1 = value

    #deleter
    @prop1.deleter
    def prop1(self):
        del self.__prop1
  
class Child(Parent):

    #getter
    @property
    def prop1(self):
        return super(Child, Child).prop1.__get__(self)

    #setter
    @prop1.setter
    def prop1(self, value):
        super(Child, Child).prop1.__set__(self, value)

    #deleter
    @prop1.deleter
    def prop1(self):
        super(Child, Child).prop1.__delete__(self)

注意! 所有属性方法必须一起重新定义。 如果不想重新定义所有方法,请使用以下模板:

class Parent():
    def __init__(self, value):
        self.__prop1 = value

    #getter
    @property
    def prop1(self):
        return self.__prop1

    #setter
    @prop1.setter
    def prop1(self, value):
        self.__prop1 = value

    #deleter
    @prop1.deleter
    def prop1(self):
        del self.__prop1


class Child(Parent):

    #getter
    @Parent.prop1.getter
    def prop1(self):
        return super(Child, Child).prop1.__get__(self)

    #setter
    @Parent.prop1.setter
    def prop1(self, value):
        super(Child, Child).prop1.__set__(self, value)

    #deleter
    @Parent.prop1.deleter
    def prop1(self):
        super(Child, Child).prop1.__delete__(self)

You can use the following template:

class Parent():
    def __init__(self, value):
        self.__prop1 = value

    #getter
    @property
    def prop1(self):
        return self.__prop1

    #setter
    @prop1.setter
    def prop1(self, value):
        self.__prop1 = value

    #deleter
    @prop1.deleter
    def prop1(self):
        del self.__prop1
  
class Child(Parent):

    #getter
    @property
    def prop1(self):
        return super(Child, Child).prop1.__get__(self)

    #setter
    @prop1.setter
    def prop1(self, value):
        super(Child, Child).prop1.__set__(self, value)

    #deleter
    @prop1.deleter
    def prop1(self):
        super(Child, Child).prop1.__delete__(self)

Note! All of the property methods must be redefined together. If do not want to redefine all methods, use the following template instead:

class Parent():
    def __init__(self, value):
        self.__prop1 = value

    #getter
    @property
    def prop1(self):
        return self.__prop1

    #setter
    @prop1.setter
    def prop1(self, value):
        self.__prop1 = value

    #deleter
    @prop1.deleter
    def prop1(self):
        del self.__prop1


class Child(Parent):

    #getter
    @Parent.prop1.getter
    def prop1(self):
        return super(Child, Child).prop1.__get__(self)

    #setter
    @Parent.prop1.setter
    def prop1(self, value):
        super(Child, Child).prop1.__set__(self, value)

    #deleter
    @Parent.prop1.deleter
    def prop1(self):
        super(Child, Child).prop1.__delete__(self)
乖乖哒 2024-08-01 05:51:01
    class Base(object):
      def method(self):
        print "Base method was called"

    class Derived(Base):
      def method(self):
        super(Derived,self).method()
        print "Derived method was called"

    d = Derived()
    d.method()

(除非我在你的解释中遗漏了一些东西)

    class Base(object):
      def method(self):
        print "Base method was called"

    class Derived(Base):
      def method(self):
        super(Derived,self).method()
        print "Derived method was called"

    d = Derived()
    d.method()

(that is unless I am missing something from your explanation)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文