python 3 中的产量生成器中没有 next() 函数 [英] there's no next() function in a yield generator in python 3

查看:30
本文介绍了python 3 中的产量生成器中没有 next() 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题中,我有一个无穷无尽的序列使用 Python 生成器.但是相同的代码在 Python 3 中不起作用,因为它似乎没有 next() 函数.next 函数的等价物是什么?

def updown(n):为真:对于范围(n)中的我:产量我对于范围内的 i (n - 2, 0, -1):产量我uptofive = updown(6)对于范围内的我(20):打印(uptofive.next())

解决方案

在 Python 3 中,使用 next(uptofive) 而不是 uptofive.next().>

内置的 next() 函数也适用于 Python 2.6 或更高版本.

In this question, I have an endless sequence using Python generators. But the same code doesn't work in Python 3 because it seems there is no next() function. What is the equivalent for the next function?

def updown(n):
    while True:
        for i in range(n):
            yield i
        for i in range(n - 2, 0, -1):
            yield i

uptofive = updown(6)
for i in range(20):
    print(uptofive.next())

解决方案

In Python 3, use next(uptofive) instead of uptofive.next().

The built-in next() function also works in Python 2.6 or greater.

这篇关于python 3 中的产量生成器中没有 next() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆