Advertisement
Fhernd

extender_propiedad_subclase.py

Dec 17th, 2018
1,596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. class Persona:
  2.     def __init__(self, nombre):
  3.         self.nombre = nombre
  4.  
  5.     @property
  6.     def nombre(self):
  7.         return self._nombre
  8.  
  9.     @nombre.setter
  10.     def nombre(self, valor):
  11.         if not isinstance(valor, str):
  12.             raise TypeError('Se requiere una cadena de caracteres.')
  13.  
  14.         self._nombre = valor
  15.  
  16.     @nombre.deleter
  17.     def nombre(self):
  18.         raise AttributeError('No se puede borrar este atributo.')
  19.  
  20.  
  21. class SubPersona(Persona):
  22.     @property
  23.     def nombre(self):
  24.         print('Recuperando nombre')
  25.         return super().nombre
  26.  
  27.     @nombre.setter
  28.     def nombre(self, valor):
  29.         print('Asignando nuevo nombre')
  30.         super(SubPersona, SubPersona).nombre.__set__(self, valor)
  31.  
  32.     @nombre.deleter
  33.     def nombre(self):
  34.         print('Borrando atributo nombre')
  35.         super(SubPersona, SubPersona).nombre.__delete__(self)
  36.  
  37.  
  38. p = SubPersona('Einstein')
  39. print(p.nombre)
  40. # Produce error:
  41. #p.nombre = 13
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement