开发者

Overwrite property using Python

开发者 https://www.devze.com 2023-01-23 11:51 出处:网络
How do you overwrite the getter of the property in Python ? I\'ve tried to do: class Vehicule(object): def _getSpatials(self):

How do you overwrite the getter of the property in Python ?

I've tried to do:

class Vehicule(object):

    def _getSpatials(self):
        pass

    def _setSpatials(self, newSpatials):
        pass

    spatials = property(_getSpatials, _setSpatials)

class Car(Vehicule)

    def _getSpatials(self):
        spatials = super(Car, self).spatials()
        return spatials

But the getter is calling the method of Vehicule and not of Car.

What should I开发者_运维百科 change ?


It looks like you want Car's spatial property's getter to call Vehicule's spatial property's getter. You can achieve that with

class Vehicule(object):
    def __init__(self):
        self._spatials = 1
    def _getSpatials(self):
        print("Calling Vehicule's spatials getter")
        return self._spatials
    def _setSpatials(self,value):
        print("Calling Vehicule's spatials setter")        
        self._spatials=value
    spatials=property(_getSpatials,_setSpatials)

class Car(Vehicule):
    def __init__(self):
        super(Car,self).__init__()
    def _getSpatials(self):
        print("Calling Car's spatials getter")
        return super(Car,self).spatials
    spatials=property(_getSpatials)

v=Vehicule()
c=Car()
print(c.spatials)
# Calling Car's spatials getter
# Calling Vehicule's spatials getter
# 1

On the other hand, calling Vehicule's setter from within Car's setter is more difficult. The obvious thing to do does not work:

class Car(Vehicule):
    def __init__(self):
        super(Car,self).__init__()
    def _getSpatials(self):
        print("Calling Car's spatials getter")
        return super(Car,self).spatials
    def _setSpatials(self,value):
        print("Calling Car's spatials setter")
        super(Car,self).spatials=value
    spatials=property(_getSpatials,_setSpatials)

v=Vehicule()
c=Car()
print(c.spatials)
c.spatials = 10
AttributeError: 'super' object has no attribute 'spatials'

Instead, the trick is to call super(Car,self)._setSpatials:

class Car(Vehicule):
    def __init__(self):
        super(Car,self).__init__()
    def _getSpatials(self):
        print("Calling Car's spatials getter")
        return super(Car,self).spatials
    def _setSpatials(self,value):
        print("Calling Car's spatials setter")
        super(Car,self)._setSpatials(value)
    spatials=property(_getSpatials,_setSpatials)

v=Vehicule()
c=Car()
print(c.spatials)
# Calling Car's spatials getter
# Calling Vehicule's spatials getter
# 1
c.spatials = 10
# Calling Car's spatials setter
# Calling Vehicule's spatials setter
print(c.spatials)
# Calling Car's spatials getter
# Calling Vehicule's spatials getter
# 10


This might help: python properties and inheritance

0

精彩评论

暂无评论...
验证码 换一张
取 消