Created
December 17, 2018 18:05
-
-
Save Fhernd/9232aac33eacab3c3fd943ea937bce7d to your computer and use it in GitHub Desktop.
Extender la propiedad de una clase. Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Persona: | |
def __init__(self, nombre): | |
self.nombre = nombre | |
@property | |
def nombre(self): | |
return self._nombre | |
@nombre.setter | |
def nombre(self, valor): | |
if not isinstance(valor, str): | |
raise TypeError('Se requiere una cadena de caracteres.') | |
self._nombre = valor | |
@nombre.deleter | |
def nombre(self): | |
raise AttributeError('No se puede borrar este atributo.') | |
class SubPersona(Persona): | |
@property | |
def nombre(self): | |
print('Recuperando nombre') | |
return super().nombre | |
@nombre.setter | |
def nombre(self, valor): | |
print('Asignando nuevo nombre') | |
super(SubPersona, SubPersona).nombre.__set__(self, valor) | |
@nombre.deleter | |
def nombre(self): | |
print('Borrando atributo nombre') | |
super(SubPersona, SubPersona).nombre.__delete__(self) | |
p = SubPersona('Einstein') | |
print(p.nombre) | |
# Produce error: | |
#p.nombre = 13 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment