2010-11-05 20 views
6

¿Cómo se sobrescribe el captador de la propiedad en Python?Sobrescribir propiedad usando Python

He tratado de hacer:

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 

Pero el captador está llamando el método de Vehiculo no de coche.

¿Qué debo cambiar?

+0

este problema sería mucho más fácil si pudiéramos usar 'Vehicule' en lugar de' súper (Coche, yo) '. Es el uso de 'super' imperativo? – unutbu

+0

¿Qué quieres decir? Puedes darme un ejemplo ? –

Respuesta

3

Parece que desea getter de la propiedad espacial de coches para llamar getter de la propiedad espacial de Vehiculo. Usted puede lograr que 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 

Por otro lado, llamando colocador de Vehiculo desde dentro colocador de coche es más difícil. La cosa obvia a hacer no funciona:

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' 

En cambio, el truco es llamar 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

Estoy usando Google App Engine, con Python 2.5 que no admite la sintaxis @ spatials.setter. –

+0

@Pierre: Oh, está bien, cambiaré mi respuesta para usar la sintaxis anterior. – unutbu

+0

Gracias unutbu. Y si quiere usar parámetros en el getter, puede usar super (Auto, self) ._ getSpatials (params) dentro de su getter en Car. –

Cuestiones relacionadas