Advertisement
Fhernd

inicializacion_estructura.py

Jan 6th, 2019
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. class Estructura:
  2.     _campos = []
  3.  
  4.     def __init__(self, *args):
  5.         if len(args) != len(self._campos):
  6.             raise TypeError('Se esperan {} argumentos'.format(len(self._campos)))
  7.  
  8.         for nombre, valor in zip(self._campos, args):
  9.             setattr(self, nombre, valor)
  10.  
  11.  
  12. class Producto(Estructura):
  13.     _campos = ['Nombre', 'Cantidad', 'Precio']
  14.  
  15.  
  16. class Punto(Estructura):
  17.     _campos = ['x', 'y']
  18.  
  19.  
  20. class Circulo(Estructura):
  21.     _campos = ['radio']
  22.  
  23.  
  24. if __name__ == '__main__':
  25.     producto = Producto('MacBook Pro', 3, 730)
  26.  
  27.     punto = Punto(0, 5)
  28.  
  29.     circulo = Circulo(3.0)
  30.  
  31.     producto = Producto('Samsung Frontier', 13, 280, 2017)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement