Python Learning notes 4--iterators, generators, adorners, recursion

Source: Internet
Author: User
Tags generator generator

iterators

Iterators are a way to access the elements of a collection. The iterator object is accessed from the first element of the collection until all of the elements have been accessed and finished. Iterators can only move forward without going backwards, but that's fine, because people seldom retreat in the middle of an iteration. In addition, one of the great advantages of iterators is that they do not require that all elements in the entire iteration be prepared in advance. An iterator computes an element only when it iterates over it, and before or after that, the element may not exist or be destroyed. This feature makes it ideal for traversing large or infinite collections, such as several G files

Characteristics:

    1. The visitor does not need to care about the structure inside the iterator, but simply continues to fetch the next content through the next () method
    2. A value in the collection cannot be accessed randomly, and can only be accessed from beginning to end
    3. You can't go back halfway through the interview.
    4. Facilitates recycling of large data sets, saving memory

To generate an iterator:

Names=iter ([11,22,33,44,55,66,77])Print(names)#only memory addresses can be displayedPrint(names.__next__())#take a value, go to the first valuePrint(names.__next__())#take a value, go to the second valuePrint('Start for') forIinchNames#take all values, actually starting from the third value    Print(i)Print('try next')Print(names.__next__())#It's at the end, and then the value is an error.Output Result:<list_iterator Object at 0x00000000006eb550>1122Start for3344556677TryNexttraceback (most recent): File"d:/winter_py/practice/winshen_python/day4/001_iter.py", line 17,inch<module>Print(names.__next__()) Stopiteration
iterators

Repeated calls to the iterator's __next__() method (or passing it to the built-in function next() ) return successive items in th E stream. When no more data was available a StopIteration exception is raised instead. At this point, the iterator object was exhausted and any further calls to its __next__() method just raise again StopIteration .

Generator Generator

Definition: When a function call returns an iterator, the function is called the Generator (generator), and if the function contains the yield syntax, the function becomes the generator

Code:

defCash_money (amount): whileAmount>0:amount-=100yield100#as long as yield is the generator, the value returned by the generator is an iterator        Print("We 're taking the money again.") ATM=cash_money (500)Print(Type (ATM))Print(ATM.__next__())Print(ATM.__next__())Print('Other thing')#To build the benefits of iterators, the function executes half after the other work, and after the work is done, proceed with the function. Print(ATM.__next__())Print(ATM.__next__())Print(ATM.__next__()) Output result:<class 'Generator'>100We 're taking the money again.100Other thing#notice where the yield is interrupted. Print again to withdraw money is to continue the time will be executed. We 're taking the money again.100We 're taking the money again.100We 're taking the money again.100
Generator

Role:

The main effect of this yield is that the function can be interrupted, and save the interrupt state, after the interruption, the code can continue to execute, after a period of time can also recall the function, from the last yield of the next sentence to start execution.

In addition, it can realize the effect of concurrency operation in single-threaded case by yield.

Python Learning notes 4--iterators, generators, adorners, recursion

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.