Python learning-iterators and generators

Source: Internet
Author: User
Tags generator iterable

1 " ". In Python, this side loop computes the mechanism, called the generator: Generator. 2 There are a number of ways to create a generator. 3 you can get the next return value for generator with the next () function" "4 5 deffib (max):6N, a, b = 0, 0, 17      whileN <Max:8         #print (b)9         yieldbTenA, B = B, A +b Onen = n + 1 A     return ' Done' -  -  the #_*_coding:utf-8_*_ - #@__author__ = ' Alex Li ' -  - Import Time + defConsumer (name): -     Print("%s ready to eat buns!"%name) +      whileTrue: ABaozi =yield at  -        Print("Bun [%s] came, eaten by [%s]!"%(baozi,name)) -  -  - defproducer (name): -c = Consumer ('A') inC2 = Consumer ('B') -C.__next__()#This allows the generator to be enabled toC2.__next__()#This allows the generator to be enabled +     Print("Lao Tzu began to prepare steamed buns!") -      forIinchRange (10): theTime.sleep (1) *         Print("made 2 buns!") $ c.send (i)Panax Notoginseng c2.send (i) -  theProducer"Alex") +  A #implementing concurrent parallel operations with generators the  + #iterators - """all objects that can be used for a For loop are iterable (iterative) types; $ all objects that can be used for the next () function are iterator (iterator) types, which represent a sequence of lazy computations; $ collection data types such as list, dict, str, etc. are iterable but not iterator, but can be via ITER () - The if function obtains a iterator object: - """

Python learning-iterators and generators

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.