It's hard to say you know something that you haven't used and don't deeply understand, and it's bound to break down when asked by others. Although there is a previous exposure to the concept of generators in python, but just a walk in the park, the two days of a conversation, others asked about generators, a moment of incoherence, dead can not think of something once read, after the sudden thought of yield, but it is too late, can only say that the concept is unclear, so this summary today wisp of python generators and yield keywords.
Before introducing yield it is necessary to explain the iterator and the constructor in Python.
In Python, for loops can be used for any type of Python, including lists, meta-ancestors, etc. In fact, for loops can be used for any "iterable object", which is actually an iterator. An iterator is an object that implements the iterator protocol, which in Python means that objects with a next method advance to the next result, and at the end of a series of results is, a Stopiteration is raised. Any such object can be iterated over in Python using a for loop or other traversal tool, which internally calls the next method at each iteration and catches StopIteration exceptions to determine when to leave.
One obvious benefit of using iterators is that they only read one piece of data from the object at a time, without causing excessive memory overhead.
II. Generator (constructor)
The generator function is associated with the concept of an iterator protocol in Python. In short, functions that contain a yield statement are compiled specifically as generators. When functions are called, they return a generator object, which supports the iterator interface. The function may have a return statement, but it is used to yield to generate the value.
Unlike normal functions that generate values and then exit, generator functions will automatically hang and suspend their execution and state after generating values, and his local variables will hold state information that will be valid again when the function resumes.