小编VRM*_*VRM的帖子

为什么我会收到此错误?类型错误:iter() 返回类型为“NoneType”的非迭代器

我正在尝试学习 __iter__ 方法,这是我收到的错误:

print(i for i in my_iter)
TypeError: iter() returned non-iterator of type 'NoneType'
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

class IterMethod:
    def __init__(self, last_elem):
        pass

    def __iter__(self):
        i = 5
        while i <= 50:
            print(f"this is {i}")
            i = i + 10
    
my_iter = IterMethod(60)

print(i for i in my_iter)
Run Code Online (Sandbox Code Playgroud)

你能解释一下为什么吗?谢谢!

python iteration

3
推荐指数
1
解决办法
7534
查看次数

标签 统计

iteration ×1

python ×1