Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created December 17, 2018 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/9232aac33eacab3c3fd943ea937bce7d to your computer and use it in GitHub Desktop.
Save Fhernd/9232aac33eacab3c3fd943ea937bce7d to your computer and use it in GitHub Desktop.
Extender la propiedad de una clase. Python.
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