Advertisement
Fhernd

atributos_gestionados.py

Dec 13th, 2018
1,407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. class Persona:
  2.  
  3.     def __init__(self, nombre):
  4.         self._nombre = nombre
  5.  
  6.     @property
  7.     def nombre(self):
  8.         return self._nombre
  9.  
  10.     @nombre.setter
  11.     def nombre(self, nuevo_nombre):
  12.         if not isinstance(nuevo_nombre, str):
  13.             raise TypeError('El tipo de dato debe ser string')
  14.  
  15.         self._nombre = nuevo_nombre
  16.  
  17.     @nombre.deleter
  18.     def nombre(self):
  19.         raise AttributeError('Este atributo no se puede borrar')
  20.  
  21.  
  22. if __name__ == '__main__':
  23.     edward = Persona('Edward')
  24.  
  25.     print(edward.nombre)
  26.  
  27.     edward.nombre = 13
  28.  
  29.     del edward.nombre
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement