2010-11-05 7 views
6

Wie überschreiben Sie den Getter der Eigenschaft in Python?Eigenschaft mit Python überschreiben

Ich habe versucht zu tun:

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 

Aber das Getter die Methode der Vehicule und nicht die von Auto ruft.

Was soll ich ändern?

+0

Dieses Problem viel einfacher wäre, wenn wir 'Vehicule' statt' Super verwenden könnte (Auto, selbst) '. Ist die Verwendung von "Super" zwingend erforderlich? – unutbu

+0

Was meinst du? Kannst du mir ein Beispiel geben ? –

Antwort

3

Es sieht aus wie Sie Getter räumlichen Eigenschaft des räumlichen Eigenschaft Getter nennen Vehicule des Autos wollen. Sie können mit

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 

Auf der anderen Seite, ruft Vehicule der Setter aus Auto Setter ist schwieriger zu erreichen, dass. Die offensichtliche Sache zu tun, nicht funktioniert:

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' 

Stattdessen ist der Trick zu nennen 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 
+0

Ich verwende Google App Engine mit Python 2.5, das die @ spatials.setter-Syntax nicht unterstützt. –

+0

@Pierre: Oh, okay, ich werde meine Antwort ändern, um die ältere Syntax zu verwenden. – unutbu

+0

Danke unutbu. Und wenn du Parameter im Getter verwenden willst, kannst du super (Car, self) ._ getSpatials (params) in deinem Getter in Car verwenden. –

Verwandte Themen