Home python Why do you need __iter__, when there __next __?

Why do you need __iter__, when there __next __?




I’m learning python and I have a question for iterators.
To be able to iterate over the object, it should be the method __ iter __ , which will return an iterator object.
In object-iterator should be the method __ next __ , which will return the next element of the object on which iterate or throwing error StopIteration .
I have met only a code in which the __ iter __ returns self .
For example

class RandomIterator:
  def __iter __ (self):
    return self
  def __init __ (self, k):
    self.k = k
    self.i = 0
  def __next __ (self):
    if self.i & lt; self.k:
      self.i + = 1
      return random ()
      raise StopIteration

Here at me and there was a question, what is the point of the method __ iter __ ?

Answer 1, Authority 100%

__ iter __ () in Python

As you learned in the lesson “Classes and Python objects”, all classes have a function called __ init __ () , which allows you to do the initialization of object creation.

Method __ iter __ () works similarly, you can perform operations (initialization and so on. D.), but he should always return an iterator object.

little Habra

Now, when we are talking about creating your own sequences in Python, it’s time to talk about the protocols. Protocols are a little similar to interfaces in other languages ​​in that they provide a set of methods that you must implement. However, Python absolutely no protocols are not binding and do not need necessarily to implement any classified. Probably, they are more like guidelines.

Why are we talking about the protocol? Because the implementation of arbitrary container types in Python entails the use of some of them. Firstly, the protocol definition immutable containers: to create an immutable container, you should only define the __ len __ and __ getitem __ (prodrobnee about them later). Minutes of the variable container requires the same as that of an immutable container, plus the __ setitem __ and __ delitem __ . And finally, if you want your objects could iterate iteration, you must define the __ iter __ , which returns an iterator. This must match the iterator iterator protocol, which requires methods __ iter __ (returns itself) and the next.

__ iter __ (self)
Must return an iterator for the container. Iterators are returned in a variety of situations, mainly for the built-in function iter () in the case of container elements busting expression for x in container: . Iterators themselves objects, they must also determine the method __ iter __ , which returns self .

UPDATE (back to the example code for asked in the comments): Here’s an example of 1st link is one of the options why. When you need to count the number of elements onto the treated or other related operations. This is useful for reducing the number of duplicate code (see. Python language principles) iter () in Python

class MyNumbers:
  def __iter __ (self):
    self.a = 1
    return self
  def __next __ (self):
    x = self.a
    self.a + = 1
    return x

And here below is my example of using the __ iter __

class Fibbonachi:
  def __iter __ (self):
    self.cur_val = 0
    self.next_val = 1
    return self
  def __next __ (self):
    tmp = self.next_val
    self.next_val + = self.cur_val 
self.cur_val = tmp
    return tmp
for i in Fibbonachi ():
  print (i)
  if i & gt; 100: break
# Result: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 per column

Answer 2, authority 38%

In general, this approach (using only __iter__ ) is much simpler in my opinion, so the question should rather sound the other way around, why is __next__ needed 🙂

class RandomIterator:
  def __iter __ (self):
    while self.i & lt; self.k:
      self.i + = 1
      yield random ()
  def __init __ (self, k):
    self.k = k
    self.i = 0

Answer 3

So my question arose, what’s the point of the iter method?

__iter__ is just good design that decouples the iterator from the actual object or container you are iterating over, you can do without it for your example.

For any questions, refer to the official literature and primary sources:
https://docs.python.org/3/tutorial/classes.html# iterators

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions