繁体   English   中英

返回类中的元组列表

[英]Return tuple list in class

如何在课堂上制作返回元组

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 中进行插入,执行executemany ,没有executemany怎么行不行。

实现类:

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函数从元组创建迭代器:

def __iter__(self):
    return iter((self.name, self.value, self.image))

或使用yield from语句来实现相同的目的:

def __iter__(self):
    yield from (self.name, self.value, self.image)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM