I'm doing OOP exercises with python and checking code I found this zip()
class pedido:
def __init__(self,productos,cantidades):
self.__productos = productos
self.__cantidades = cantidades
def total_pedido(self):
total = 0
for(p,c) in zip(self.__productos,self.__cantidades):
total = total + p.calcular_Total(c)
here the method that shows the data
def mostrar_pedido(self):
for(p,c) in zip(self.__productos,self.__cantidades):
print("producto",p.nombre,"cantidad"+str(c))"""
What the function does
zip()
is "package" the contents of the brackets, if you realize it is passing two parameters, this is quite useful to make more optimal and clean code and to avoid gimmicky functionsIf we analyze that piece of code we have that the variable
p
is corresponding toself.__productos
while the variablec
is corresponding toself.__cantidades
in other words, it lets you iterate over the two objects side by side.You can find more information in Python zip() Function