简体   繁体   English

Python属性和方法覆盖问题:为什么子类属性仍然调用基类的方法

[英]Python property and method override issue: why subclass property still calls the base class's method

Here is an example 这是一个例子

class A(object):
        def f1(self):
                return []
        test1 = property(f1)


class B(A):
        def f1(self):
                return [1, 2]

if __name__ == "__main__":
        b = B()
        print b.test1

I expect the output to be [1, 2], but it prints [] instead. 我希望输出为[1,2],但它会打印[]。

It is contrary to my expectation. 这与我的期望相反。

Did I make any mistake in the code? 我在代码中犯了什么错误吗? If not, I suppose it works this way because when the property test1 is created, it is bound to the f1 function of the base class A. What is a possible alternative implementation to achieve what I want? 如果没有,我认为它是这样工作的,因为当创建属性test1时,它被绑定到基类A的f1函数。有什么可能的替代实现来实现我想要的?

You can defer the lookup of f1 with a lambda function if you don't wish to pollute the class namespace 如果您不希望污染类命名空间,可以使用lambda函数推迟f1的查找

class A(object):

        def f1(self):
                return []

        test1 = property(lambda x:x.f1())

I suppose it works this way because when the property test1 is created, it is bound to the f1 function of the base class A. 我认为它的工作原理是这样的,因为当创建属性test1时,它被绑定到基类A的f1函数。

Exactly correct. 完全正确。

What is a possible alternative implementation to achieve what I want? 什么是可能的替代实现来实现我想要的?

One more level of indirection: 另一个层次的间接:

class A(object):
    def f1(self): return []
    def _f1(self): return self.f1()
    test1 = property(_f1)

class B(A):
    def f1(self): return [1, 2]

A couple of alternatives I can think of: either repeat the call to property in the subclass, 我能想到的几个替代方案:要么重复对子类中property的调用,

class B(A):
    def f1(self):
        return [1,2]
    test1 = property(f1)

or base the property on another method which calls f1 : 或者将该属性基于另一个调用f1方法:

class A(object):
    def f1(self):
        return []
    def _f1(self):
        return self.f1()
    test1 = property(_f1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么子类会覆盖基类方法,但使其完全相同(而不使用它)? - Why a subclass would override a base class method but make it identical (and not use it)? 如何使用方法有条件地覆盖Python类属性 - How to conditionally override a Python class property with a method 如何在基类的方法中访问子类的重写的类属性? '__class__'仍然指向基类 - How to access a child class's overridden class property in a base class's method? '__class__' still point to the base class 覆盖子类中的属性设置器抽象方法 - Override property setter abstract method in child class 如何将通过重写方法从子类计算的值返回到python中的基类方法 - How to return a value computed by overriding method from a subclass to it's base class method in python Python 子类覆盖了具有不同签名的方法,因此不会调用基类方法。 这是为什么? - Python subclass overridden method with different signature, so base class method is not called. Why is that? 如何覆盖子类中基类的静态抽象方法? - How to override static abstract method of base in a subclass? Python:为什么将子类方法的 **kwarg 作为参数传递到基础 class 的 __call__() 签名中? 结果类型错误 - Python: Why would a **kwarg of a subclass method be passed as a parameter in __call__() signature of the base class? Results in TypeError Python超级:基类方法调用另一个方法 - Python super: base class method calls another method Python多态对象调用基类方法而不是子类方法 - Python polymorphic objects calling base class methods instead of subclass method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM