简体   繁体   English

Python-__next __()方法中的索引如何工作?

[英]Python - How does the index in the __next__() method work?

Can someone explain to me how the __iter__() and __next__() functions handle indices? 有人可以向我解释__iter__()__next__()函数如何处理索引吗? Are they base 0 or base 1 ? 它们是以0底还是以1底?

I have been playing around with it, but I'd like to know what Python is actually doing on the back end. 我一直在玩它,但是我想知道Python在后端实际上在做什么。 I tried the example class below: 我尝试了下面的示例类:

>>> class my_class:
        def __init__(self, *stuff):
            self.__stuff = stuff

        def __iter__(self):
            self.__n = 0
            return iter(self.__stuff)

        def __next__(self):
            if self.__n <= len(self.__stuff):
                self.__n += 1
                return self.__stuff(self.__n)
            else:
                raise StopIteration

>>> x = my_class(1, 2, 3, 4)
>>> for each in x:
        print(each)
1
2
3
4

Unless, I'm mistaken, the first self.__n value that __next__() uses should be 1 , which should produce, this: 除非我弄错了,否则__next__()使用的第一个self.__n值应该为1 ,这应该产生以下内容:

>>> for each in x:
        print(each)
2
3
4

What am I missing? 我想念什么? How does it know to start at self.__stuff[0] ? 如何知道从self.__stuff[0]

When you call for each in x: , it do nothing with __next__() in your class definition, so it start 1 of your object attribute rather than 2. 当您for each in x:调用for each in x: ,它对类定义中的__next__() ,因此它以对象属性的1开始而不是2开始。

Even it you want to call something like print(next(x)) it will give you 'TypeError: 'tuple' object is not callable', because self.__stuff(self.__n) is invalid as in self.__stuff is a tuple and self.__n is an integer. 即使您想要调用诸如print(next(x))东西,它也会给您'TypeError:'tuple'object is not callable',因为self.__stuff(self.__n)self.__stuff一样是无效的self.__stuff是一个元组self.__n是一个整数。 You can only call tuple[int] rather than tuple(int) . 您只能调用tuple[int]而不能调用tuple(int)

Try following code after your code mentioned it will return you desired output then raise an exception. 提及代码后,请尝试以下代码,它将返回所需的输出,然后引发异常。

for each in x:    
    print(next(x))

Result: 结果:

2
3
4
raise StopIteration
  • When you use my_class , it first calls the __init__ , then calls the __iter__ , last is the __next__ . 当您使用my_class ,它首先调用__init__ ,然后调用__iter__ ,最后是__next__
  • In your code, when it calls __iter__ , it return iter(self.__stuff) ,then is over, __next__ is not called. 在您的代码中,当它调用__iter__ ,它return iter(self.__stuff) ,然后结束,不调用__next__ So the output is what you see. 所以输出就是您所看到的。
  • If you want __next__ called, you can change your code like this(here self.__n that __next__ uses starts from 1): 如果要__next__调用,则可以这样更改代码(此处是__next__使用的self .__ n从1开始):

     class my_class: def __init__(self, *stuff): self.__stuff = stuff def __iter__(self): self.__n = 0 print('__iter__ is called') return self def __next__(self): print('__next__ is called') if self.__n <= len(self.__stuff): self.__n += 1 return self.__stuff(self.__n) else: raise StopIteration 
  • Tip: you can use print to help you understand what the code is doing, like print function in the code above. 提示:您可以使用print来帮助您了解代码的功能,例如上面代码中的print功能。

The __iter__() method returns iter(self.__stuff) instead of self . __iter__()方法返回iter(self.__stuff)而不是self As such, the tuple passed to __init__() is iterated over, not the object. 这样,将遍历传递给__init__()的元组,而不是对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM