python - 返回类中的元组列表
问题描述
如何在课堂上制作返回元组
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
中插入,没有交互器怎么办?
实现类:
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)
推荐阅读
- python - 创建具有实际值的虚拟列
- angular - Angular :mat-input 输出在输入值更改时触发两次
- r - 对 data.frame 中的后续条目(重复)求和
- r - 查找节点邻居的度值之和
- java - OpenCV人脸识别Java代码终止JVM
- python - 使用 If 条件迭代字典的函数
- python - 测试因浮点限制导致的舍入误差
- php - 从 php 中的其他表在大型 mysql 表中添加新的关系列
- javascript - 使用 Firebase 规则构建数据
- java - Android 相机在 APi 22(Lollipop)上失败,但在所有其他版本上都可以使用