__iter__ Python · 特殊方法
让对象可以被 for 循环逐个遍历。
怎么用
def __iter__(self):
小例子
class 数列:
def __iter__(self):
return iter([1, 2, 3])
for x in 数列():
print(x)
运行结果
1 2 3
官方文档:https://docs.python.org/zh-cn/3/reference/datamodel.html#object.__iter__
在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条