我看到next = __next__出现在OOP代码中的多个地方。
这样做的目的是什么?没有这条线对我也很管用。
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)发布于 2020-04-23 18:35:04
这是为了与Python2.per Python3.0的新功能是什么?向后兼容
佩普3114:标准的
next()方法已重命名为__next__()。
https://stackoverflow.com/questions/61394467
复制相似问题