Python iterator, generator __python

Source: Internet
Author: User

What's the iteration :

    An object that can be used directly as a for loop is called an iterative object, and objects that can be called by Next () function, and that return the next value continuously, are called iterators, and
all iterable can be converted to iterator by the built-in function iter (). 
    for iterators, having a next () is enough. When you use the for and in statements, the program automatically invokes the iteration object of the object that is about to be processed, and then uses the next () method.

* * Replication Iterator * *

We need to use the deepcopy in copy.

Example:
    >>> import copy
    >>> i=iter (L)
    >>> j=copy.deepcopy (I)
    >>> Next (i)
    1
    >>> Next (i)
    2
    >>> Next (J)
    1
    '


* * * Generator *
Instead of saving the result in a series, the generator saves the state of the generator and returns a value each time the iteration is performed, until the stopiteration exception is encountered.
Builder function: If the yield keyword appears in a function, then the function is no longer a normal function, but a generator function.
    But the generator function can produce a wireless sequence, so the list simply does not have a way to handle it.

    the role of yield is to turn a function into a generator, a function with yield is no longer a normal function, and the Python interpreter treats it as a generator.
**yield and return**:
In a generator, if there is no return, the default is performed to the end of the function and returns to Stopiteration, and if you are met, throw stopiteration to terminate the iteration if it is returned during
execution.
* * Summary * *:

A builder is an iterator that can be iterated using a for loop
"'

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.