Python Foundation 13 iterators and generators

Source: Internet
Author: User
Tags generator iterable

before?? Can - iterators and generators
The main contents of this section :
1. iterators
2. builder

? . iterators
We were in front of you. Iterate the object into the iteration operation . So what exactly is an iterative object . this section mainly
An iterative object . First, let's look at the following: What are the iterative objects we know before?
STR, list, tuple, Dict, set. So why can we call them an iterative object ? because they all followed the possible
Iteration protocol . What is an iterative protocol . let's look first. Segment error code :

Note that there is such a word in the wrong message . ' int ' object is not iterable. translated is an integer type object.
is non-iterative . iterable table? can be iterated . table? An iterative protocol . so how to enter? Verify that your data type is
Conform to an iterative protocol . we can use the Dir function to see all the methods defined in the class .

we found that __iter__ can be found in the string . keep looking? Next list, tuple, dict, set

 we found this one to go in? forthere's a cycle of east.__iter__function, includeRangealso have. OK?? Try?
Under.
In comprehensive.We can be sure.if the object has__iter__function. Then we think this object adheres to the iterative protocol
this __iter__ we make? __next__ () to get the
element in an iterator So what we said earlier for What is the principle? continue to see code

? . ? Builder
What is the builder . The Builder is essentially an iterator .
There are three ways to get the generator in Python :
1. Pass the "generator" function
2. using a variety of derivation to achieve the generator
3. the conversion of data can also be obtained by the generator
Today , we mainly make?? Generator function to complete the creation and making of the generator? .
First , Let's look at a very simple function :

The results of the shipment and the above?. What is because there is yield in the function. then this function is the generator
function this time When we take this function again, is this the builder How to make it so We can do it directly? __next__ () The following builder

Example:

  When the program is finished, the last one yield. So after? continue in? __next__ () program will error okay? The builder says Let's take a look at this. Demand Company to jack jones order
Purchase 10000 sleeve learn to serve made directly 10000 suit

But the company does not have so many people now ah, you give me 10,000 sets I have no place to save Ah, very embarrassed,

The perfect thing is that I use a set you give me a set of

difference No. The species is direct, the sub-sex is all taken out . will take up? Memory The first species to make?? Generator less than how many will not go back to where Pointer to where?

---->>>> Send and __next__ differences <<<<<-----

First execution must be done with __next__

This is messy, you can run it yourself see the results first print and then print yield return value then send value into the last yield print out

Python Foundation 13 iterators and generators

Related Article

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.