2010-11-05 20 views
6

Come sovrascrivi il getter della proprietà in Python?Sovrascrivi la proprietà usando Python

Ho provato a fare:

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 

Ma il getter chiama il metodo di Vehicule e non di auto.

Cosa devo cambiare?

+0

questo problema sarebbe molto più facile se si potesse usare 'Vehicule' invece di' super (auto, auto) '. L'uso di 'super' imperativo? – unutbu

+0

Cosa intendi? Puoi darmi un esempio? –

risposta

3

Sembra che si desidera getter della struttura spaziale di auto per chiamare getter della struttura spaziale di Vehicule. È possibile ottenere che con

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 

D'altra parte, chiamando setter di Vehicule dall'interno setter di auto è più difficile. La cosa più ovvia da fare non funziona:

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' 

Invece, il trucco è quello di chiamare 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

Sto utilizzando Google App Engine, con Python 2.5 che non supporta la sintassi @ spatials.setter. –

+0

@Pierre: Oh, okay, cambierò la mia risposta per usare la sintassi più vecchia. – unutbu

+0

Grazie unutbu. E se vuoi usare i parametri nel getter, puoi usare super (Car, self) ._ getSpatials (params) nel tuo getter in Car. –