简体   繁体   English

财产设定者和继承

[英]property.setter and inheritance

I have two classes, BaseClass and Subclass (where Subclass is a subclass of BaseClass). 我有两个类,BaseClass和Subclass(其中Subclass是BaseClass的子类)。 In each of these classes, I have a property 'foo' which I'd like to be able to get and set. 在每个此类中,我都有一个属性“ foo”,我希望能够获取和设置该属性。 The way in which 'foo' is set is independent of the class - it's done the same way in both BaseClass and Subclass. 设置'foo'的方式与类无关-在BaseClass和Subclass中都采用相同的方法。 However, the way in which we 'get' the property 'foo' is dependent on the class. 但是,我们“获取”属性“ foo”的方式取决于类。 I would thus like a way to define my 'setter' on BaseClass and have it inherit to Subclass in such a way that it's compatible with the class-dependent implementations of the 'getter' in both Subclass and BaseClass. 因此,我想一种在BaseClass上定义我的'setter'并将其继承到Subclass的方式,使其与Subclass和BaseClass中'getter'的依赖于类的实现兼容。

My first instinct was to use python @property and @property.setter. 我的第一个本能是使用python @property和@ property.setter。 I quickly ran into issues: 我很快遇到了问题:

class BaseClass(object):
    def __init__(self):
        self._foo = None

    @property
    def foo(self):
        print 'BaseClass'

    @foo.setter       
    def foo(self, val):
        self._foo = val


class Subclass(BaseClass):

    @property
    def foo(self):
        print 'Subclass'

My naive hope was that the foo.setter would be inherited into Subclass and that it would be compatible with the Subclass implementation of the foo property. 我的天真希望是foo.setter将被继承到Subclass中,并且它将与foo属性的Subclass实现兼容。 However: 然而:

b = BaseClass()
s = Subclass()
b.foo
BaseClass
s.foo
Subclass
b.foo = 'BaseClass!'
s.foo = 'Subclass!'
Traceback (most recent call last):
 File "<input>", line 1, in <module>
AttributeError: can't set attribute

I believe what is happening here is that the '@foo.setter' is being bound to the BaseClass namespace at compile time, and thus is not available to Subclass (although I could be wrong on this point). 我相信这里发生的是'@ foo.setter'在编译时绑定到BaseClass命名空间,因此Subclass不可用(尽管我可能在这一点上是错的)。

Can anyone tell me a neat way of achieving what I want here? 谁能告诉我在这里实现我想要的东西的巧妙方法? It doesn't necessarily need to use python property builtin, but that would be nice. 不一定需要使用内置的python属性,但这会很好。

There are some interesting things going on here. 这里发生了一些有趣的事情。 BaseClass.foo is an object, and the lines BaseClass.foo是一个对象,行

@foo.setter       
def foo(self, val):
    self._foo = val

return a modified copy of that object. 返回该对象的修改后的副本。

In the SubClass, you are redefining this object by recreating the property and so you will need to write a new setter or follow Aran-Fey's answer. 在SubClass中,您将通过重新创建属性来重新定义此对象,因此您将需要编写一个新的setter或遵循Aran-Fey的回答。

I found a similar question that might also help with understanding this idea. 我发现了一个类似的问题 ,也可能有助于理解这个想法。

You can use @BaseClass.foo.getter to create a copy of the property with a different getter: 您可以使用@BaseClass.foo.getter创建具有不同getter的属性的副本:

class Subclass(BaseClass):
    @BaseClass.foo.getter
    def foo(self):
        print('Subclass')

See the property documentation for details. 有关详细信息,请参见property文档

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM