Why does a Python Iterator need an iter method that simply returns self?

后端 未结 4 840
面向向阳花
面向向阳花 2021-01-06 05:35

I understand that the standard says that it does but I am trying to find the underlying reason for this.

If it simply always returns self what is the ne

4条回答
  •  花落未央
    2021-01-06 06:31

    It's so for loops and other code that needs to work with iterables can unconditionally call iter on the thing they're iterating over, rather than treating iterators and other iterables separately. In particular, non-iterators might reasonably have a method called next, and we want to be able to distinguish them from iterators.

提交回复
热议问题