Interview essential | with you completely get to know Python generator.

Written before

High-level language features of Python has been a difficulty we learn Python, most people do not do skilled master, even to learn it all feels very difficult, "builder" as the one which is very useful feature, it is in this way.

Because the concept is not generated in other mainstream languages ​​(C / C ++ / JAVA), the combined with a certain degree of difficulty, time spent learning up large costs, a lot of people's self-comforting them as "tasteless" then decisively abandoned so useful characteristic of a high-level language, it is a very regrettable thing.

Fact, not only for the "generator" for other high-level language features or suggest that you take the time to get to know, do not say other content, as these things often test interviews should catch your attention, after all, the company not stupid, useless things to test you doing?

Next, let's learn together "generator", I try to listen with everyone to understand, then, to explain the progressive layers to ensure that everyone can understand, the premise is to have patience, a long article that first collection look.

Promise me, you must be patient.

Iterator

In saying this reason "iterator" is " Builder" automatically the "iterator protocol", the so-called agreement is a convention . To better understand the generator, we simply need to know about "the iterator protocol" in the end is what. In fact, only two need to meet two conditions: 1. achieve __iter__ method; 2. Object achieve next () method, or return next iteration, or is to StopIteration abnormal termination iteration.

The object is the "iterable", that is, objects that implement the iterator protocol, it implements the iterator protocol. In fact, like Python, a for loop, sum functions, and so is the use of the iterator protocol to access the object.

You might look a bit ignorant, how is "iterative" is "iterator" is "iterable", which is a very abstract concept for all of us, but do not be afraid, my article a long time ago in has introduced a very detailed and maybe man, you just need to click on the link below to see just fine, this is the interview common problem oh:

Builder

If you understand the contents of one, then congratulations to you, then learn "generator" would be much simpler. Python using generators to "delay the operation," provides support for the so-called "delay operation" is the only produce results when it is needed, rather than that produced immediate results.

Then we are talking about the equivalent of an extension of the above article and re-expand.

Python fact, there are two different ways to provide generators, one is the functional form, is another form of expression, said the whole bit is "generator function" and "generator expression."

1. The generator function

"Generator function" and the general definition of a function similar. Except that normal function return a result using the return, the generator function is yield  return results.

yield effect is to return the call at the time corresponding to the value, one returns a result (i.e., suspended) in the middle of each result pending status of the function, the next execution is started from the last pause position, continues down .

Let's do a question, asked to write "a whole list of integers is only reserved after the operation odd." I believe most people can quickly write such a function of the following:

 

The above is nothing more difficult, since we learned "generator", so I lick it in the front, which is not what we do with the builder about this question? Look to do the same function with the generator, in the end what is the difference:

 

Compare two approaches to this function, the use of "builder" after the number of lines of code becomes less of (eliminating the need for the operation of the res, the results do not exist in res), the code overall look clearer (a why see that, I do not think one up res is what the hell, append go What the hell is).

2. The generator expression

"Expression Builder," and list comprehensions similar. Except that a list of derivation , one will have all the results, but with the "generator expression" is not the case, it is produced on demand.

List comprehension is worded as follows:

 

The above expression is the generator of [] becomes () to:

 

We also look at the way a simple "builder" advantage "generator expression" is how embodied. If we want to sum a series of integers, the direct use of the generator can be written as follows:

 

Of course, for convenience, parentheses may be omitted, i.e. written as follows:

 

But if you use regular writing to write, it will be written as follows:

 

The code above to construct a list, and then summing the sum function, multi-step, different as day, time efficiency of light, it has lost the pants.

So overall the above article talked about, "generator" light, there are several advantages in the clear surface: fewer lines of code; the code more readable; higher aging ...

So, you dare to see it as "tasteless" it?

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/94762888