Python Programming Series---iterative objects, iterators, and generators

Source: Internet
Author: User
Tags generator iterable

One or three features on the code

1. An object that has a __iter__ method is an iterative Class (object)
2. There is a __iter__ method, the object of the __next () method is an iterator
3. Generator = = function +yield

Generators belong to iterators, iterators are special, iterative objects

Ii. the respective implementation process 1. Iterative Object Execution Process

1. first call to the ITER () function : iter (iterable), iterator: The function passes in an iterative object (iterable), and returns an iterator (iterator);
Internally, an iterator (iterator) is obtained through an iterative object (iterable) of the __iter__ ()

 2. call the next () function again : Next (iterator)->return the next item from the iterator: the function passes in an iterator (iterator), returning the next value that the iterator points to;

The interior is the next value (IItem) obtained by __next__ () of the iterator (iterator)

The code is as follows:

1 #defines an object that can be iterated, such as a list2List1 = [1, 2, 3, 4, ]3 4 #generate an iterator via ITER ()5List_iterator =iter (List1)6 7 #gets the next element that the iterator points to by next (), starting with the first8 Print("the 1th iteration of an element:", Next (list_iterator))9 Print("the 2nd iteration of an element:", Next (list_iterator))Ten Print("the 3rd iteration of an element:", Next (list_iterator)) One Print("The 4th iteration of an element:", Next (list_iterator)) A  -  -  the The results are as follows: -The 1th iteration of the element: 1 -The 2nd iteration of the element: 2 -The 3rd iteration of the element: 3 +The 4th iteration of the element: 4 -  +Process has ended, exit code 0

Add:

List Ganso is an iterative object, but not an iterator, although you can call the internal __iter__ method, return an iterator, but not itself (itself is not a generator), so you cannot directly call next () to return the next value that the iterator points to, you must first call the ITER () method

2.yield implementing the Generator execution process

1. If there is yield in the function, it is no longer a function, but a generator
2. Yield produces a breakpoint, pauses the function, suspends the function, and saves the current state
3. If the yield is followed by a data, the data will be returned,
As next () function or for ... in ... Iteration of the next value
4. You can wake the generator with next () to allow the generator to continue executing at the breakpoint

The code is as follows:
defFibo (n):"""using yield implementation generators to find Fibonacci sequences"""Count= 0#records the position of the current iteration, with an initial value of 0NUM1, num2 = 0, 1#the first two-bit value of the initialization sequence 0,1     whileCount <N:yieldnum1 num1, num2= num2, Num1 +num2 Count+ = 1#from add one, the iterator pointer points to the next position#To create the generator, the generator is dormant by default, and if it is the first wake-up generator (function), run from the start of the generator (function)Gen = Fibo (10)#use Next () IterationPrint("The value returned:", Next (gen))Print("The value returned:", Next (gen))Print("The value returned:", Next (gen))Print("The value returned:", Next (gen))Print("The value returned:", Next (gen))Print("The value returned:", Next (gen))Print("The value returned:", Next (gen))Print("The value returned:", Next (gen))Print("The value returned:", Next (gen))Print("The value returned:", Next (gen)) results as follows: The returned value: 0 The value returned:1The value returned:1The value returned:2The value returned:3The value returned:5The value returned:8The value returned:13The value returned:21stThe value returned:34process has ended, exit code1

Three, summed up the three to get the data of the iterative method is as follows:

  Generator: You can use next ()/send ( ) to get the data
Iterators: You can use next () to get the data
Iterate objects (only iterative objects): Use iter () +next ( ) to get data

Python Programming Series---iterative objects, iterators, and generators

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.