首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >next = __next__做什么?

next = __next__做什么?
EN

Stack Overflow用户
提问于 2020-04-23 18:29:50
回答 1查看 300关注 0票数 3

我看到next = __next__出现在OOP代码中的多个地方。

这样做的目的是什么?没有这条线对我也很管用。

代码语言:javascript
复制
class Iter:
    def __init__(self, value):
        self.data = value


    def __iter__(self):
        self.ix = 0
        return self

    def __next__(self):
        if self.ix == len(self.data):
            raise StopIteration

        else:
            item = self.data[self.ix]
            self.ix += 1
            return item

    next = __next__

if __name__ == '__main__':
    x = Iter([1,2,3,4,5,6,7,8,9 ])
    for i in x:
        print(i)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-23 18:35:04

这是为了与Python2.per Python3.0的新功能是什么?向后兼容

佩普3114:标准的next()方法已重命名为__next__()

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61394467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档