Understand the iterator and iterable

Preface:

Before iterator iterable this section it has been some confusion, with some information, to express my understanding of these concepts may not have to, but for the beginning of my friends to start from scratch to understand


 

Straight to the point first introduced iterables popular understanding and iterators

  An iterator is just be next () call to get the next iteration of the value of the object, the iterator does not directly save the sequence value iteration, and iteration of the algorithm stored value obtained under

  Iterables is to be iter () method call to get iterable objects, only iterables available for a for loop

 

The principle underlying the for loop:

  The following is a for loop format

 

  for I in iterables: 
    loop

  The method essence is to call a built-ITER () obtained iterator object, then the iteration value i is obtained by each next (iterator) call

  For example the following code:

for i in x:
    print(i)

  It can be rewritten as:

iterator = iter(x)
while True:
    i = next(iterator)
    print(i)

 

Strictly speaking, or underlying principle

  Iterator or iterator object: implements the __next __ () method Magic (class instantiated) object, the method returns the next value iterator

  Iterables: __ implements the __iter magic methods (class instantiated) object (), which returns an iterator object

  

In fact, next and iter is a further package __next__ and __iter__ or say next and iter when they are called to perform the features within __next__ and __iter__ in order to achieve the iterator and iterable and function

 

In summary, we can understand the for loop, iterables, iterators three contact:

  The for loop iterations can only be objects, iterables they need to implement the iterator

Thus, we can implement a custom iterators and iterator object, in order to achieve the effect of the cycle for custom 


 

Example:

Fibonacci series to achieve cut through the iterator and iterable

They are defined iterator class and object belongs iteration:

class On Feb: 
  '' 'may be defined in the object's class iteration' ''
DEF the __init__ (Self, NUM): indicates the length of the number of the #NUM column   self.a =. 1   self.b, = 2
     self.current = self.a
     self.num NUM = DEF the __iter__ (Self): return FebIterator (Self) class FebIterator (Self):
  '' 'defined iterator class' ''
DEF the __init__ (Self, Source): self.source = Source DEF __next__ (Self):
     
     IF ( . 1-self.num> = 0):
       self.num = self.num. 1-     self.current
= self.a     Self.a = Self.b,     self.b, = + self.current self.b, # above two intermediate variable assignment may be omitted to directly write self.a, self.b, self.b, =, + self.a Self, B
       return Self. curent
     the else: The raise StopIteration

By calling these two classes to instantiate objects can be achieved circulation print Fibonacci number

feb = Feb()
for i in feb: print(i)

In fact, for simplicity and convenience, can simultaneously achieve the __iter __ () and the __next __ () method in a class, the class that is instantiated objects is only one iteration iterator objects, as follows:

class Feb:
    def __init__():
        self.a = 1
        self.b = 2
     sele.current = self.a def __iter__(self): return self def __next__():
     if(self.num-1>=0):
       = self.num. 1-self.num 
        self.current = self.a 
        self.a = self.b,      self.b, self.current # + = self.b, above two intermediate variable assignment may be omitted to directly write self.a , self.b, self.b, =, + self.a Self, B 
       return self.curent
     the else: The raise the StopIteration
 

Prior to its use and there is no difference, but more simple, provided there is a clear understanding and awareness of the iterator and iterable, or would confuse the two concepts

Guess you like

Origin www.cnblogs.com/burningcarbon/p/11567872.html