Created
January 6, 2019 17:50
-
-
Save Fhernd/85be0b6706e60ced5c865686f5c30bca to your computer and use it in GitHub Desktop.
Incialización de estructuras de datos. 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 Estructura: | |
_campos = [] | |
def __init__(self, *args): | |
if len(args) != len(self._campos): | |
raise TypeError('Se esperan {} argumentos'.format(len(self._campos))) | |
for nombre, valor in zip(self._campos, args): | |
setattr(self, nombre, valor) | |
class Producto(Estructura): | |
_campos = ['Nombre', 'Cantidad', 'Precio'] | |
class Punto(Estructura): | |
_campos = ['x', 'y'] | |
class Circulo(Estructura): | |
_campos = ['radio'] | |
if __name__ == '__main__': | |
producto = Producto('MacBook Pro', 3, 730) | |
punto = Punto(0, 5) | |
circulo = Circulo(3.0) | |
producto = Producto('Samsung Frontier', 13, 280, 2017) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment