如何在类中生成返回元组
class Product:
def __init__(self, name, value, image):
self.name = name
self.value = value
self.image = image
def __iter__(self):
return (self.name, self.value, self.image)
我需要在MySql witchexecutemany
中做insert,怎么都没有interator不工作。你知道吗
实现类:
from Product import Product
Products = []
Product_name = "Fruit 01"
Product_price = 12.25
Product_image = "src/test.png"
Products.append(Product(
Product_name,
Product_price,
Product_image
))
__iter__
方法应该返回一个迭代器。你知道吗您可以使用
iter
函数从元组创建迭代器:或者使用
yield from
语句实现相同的功能:相关问题 更多 >
编程相关推荐