Herencia Parte 1
Herencia Parte 1
In [15]:
# Herencia
class Producto():
def __str__(self):
return """\
Referencia: \t {}
Nombre: \t {}
Precio: \t {}
Descripción: \t {}
""".format(self.referencia, self.nombre, self.precio, self.descripcion)
In [16]:
class Adorno(Producto):
pass
In [17]:
Referencia: 1234
Nombre: Adorno 1
Precio: 23
Descripción: Adorno para mesas de salón
In [20]:
class Alimento(Producto):
productor = ""
distribuidor = ""
def __str__(self):
return """\
Referencia: \t {}
Nombre: \t {}
Precio: \t {}
Descripción: \t {}
Productor: \t {}
Distribuidor: \t {}
""".format(self.referencia, self.nombre, self.precio, self.descripcion, self
https://hub.gke.mybinder.org/user/ipython-ipython-in-depth-4ihr4zq3/notebooks/binder/Untitled.ipynb?kernel_name=python3 1/3
22/8/2019 Untitled
In [23]:
Referencia: 1342
Nombre: Melon
Precio: 4
Descripción: Piel de Sapo
Productor: Productor del sur
Distribuidor: Todo frutas
In [27]:
class Libro(Producto):
isbn = ""
autor = ""
def __str__(self):
return """\
Referencia: \t {}
Nombre: \t {}
Precio: \t {}
Descripción: \t {}
ISBN: \t \t {}
Autor: \t \t {}
""".format(self.referencia, self.nombre, self.precio, self.descripcion, self
In [28]:
libro1 = Libro(2324, "Harry Potter y la piedra filosofal", 12, "Mago que vive...")
libro1.isbn = "KDAKDJ-ADJKD-334-3J3-J3"
libro1.autor = "J.K. Rowling"
print(libro1)
Referencia: 2324
Nombre: Harry Potter y la piedra filosofal
Precio: 12
Descripción: Mago que vive...
ISBN: KDAKDJ-ADJKD-334-3J3-J3
Autor: J.K. Rowling
In [29]:
In [30]:
productos
Out[30]:
[<__main__.Adorno at 0x7ff4c8d82588>,
<__main__.Alimento at 0x7ff4c8db1c50>,
<__main__.Libro at 0x7ff4c8d82320>]
https://hub.gke.mybinder.org/user/ipython-ipython-in-depth-4ihr4zq3/notebooks/binder/Untitled.ipynb?kernel_name=python3 2/3
22/8/2019 Untitled
In [31]:
for p in productos:
if (isinstance(p, Alimento)):
print(p.nombre, p.distribuidor)
elif (isinstance(p, Libro)):
print(p.nombre, p.autor)
else:
print(p.nombre)
Adorno 1
Melon Todo frutas
Harry Potter y la piedra filosofal J.K. Rowling
In [37]:
In [38]:
rebajar_producto(libro1, 80)
In [39]:
print(libro1)
Referencia: 2324
Nombre: Harry Potter y la piedra filosofal
Precio: 2.4000000000000004
Descripción: Mago que vive...
ISBN: KDAKDJ-ADJKD-334-3J3-J3
Autor: J.K. Rowling
In [ ]:
https://hub.gke.mybinder.org/user/ipython-ipython-in-depth-4ihr4zq3/notebooks/binder/Untitled.ipynb?kernel_name=python3 3/3