Python learning fourth iterator builder for process programming

Source: Internet
Author: User

1: What is an iteration

in statement, the program automatically invokes the iterator object of the object being processed, and then uses its next__ () method until a Stopiteration exception is detected.

>>> L = [n/A]
>>> [x**2 for x in L]
[1, 4, 9]
>>> Next (L)
Traceback (most recent):
File "<stdin>", line 1, in <module>
TypeError: ' List ' object is a iterator
>>> I=iter (L)
>>> Next (I)
1
>>> Next (I)
2
>>> Next (I)
3
>>> Next (I)
Traceback (most recent):
File "<stdin>", line 1, in <module>
Stopiteration

#1, why do you have iterators? for sequence types: strings, lists, tuples, we can iterate over the elements that they contain by using an indexed method. But for dictionaries, collections, files, and so on, there is no index, and if you want to take out the elements that are contained inside, you must find an iterative way that does not depend on the index, which is the iterator # #, what is an iterative object? An iterative object refers to an object with a built-in __iter__ method, namely obj.__iter__, as follows ' Hello '. __iter__ (All-in-one). __iter__[1,2,3].__iter__{' A ': 1}.__iter__{' a ', ' B '}.__iter__open (' a.txt '). __iter__#3, what is an iterator object? The result of an iterative object execution obj.__iter__ () is an iterator object, whereas an iterator object refers to an object file type that has a built-in __iter__ and built-in __next__ method is an iterator object open (' A.txt '). __iter__ () Open (' A.txt '). __next__ ()#4, note: the iterator object must be an iterative object, and an iterator object is not necessarily an iterator object



2: Use of Iterator objects

Dic={' A ': 1, ' B ': 2, ' C ': 3}
ITER_DIC=DIC.__ITER__ () #得到迭代器对象, the iterator object is __iter__ and __next__, but the iterator. __iter__ () still gets the iterator itself
Iter_dic.__iter__ () is Iter_dic #True

Print (iter_dic.__next__ ()) #等同于next (Iter_dic)
Print (iter_dic.__next__ ()) #等同于next (Iter_dic)
Print (iter_dic.__next__ ()) #等同于next (Iter_dic)
# Print (iter_dic.__next__ ()) #抛出异常StopIteration, or End flag

#有了迭代器, we can not rely on the index iteration to take the value
ITER_DIC=DIC.__ITER__ ()
While 1:
Try
K=next (Iter_dic)
Print (Dic[k])
Except stopiteration:
Break

#这么写太丑陋了, we need to catch the anomaly ourselves, control next,python so cool, can you help me solve it? Can, see for loop

3:for Cycle

#基于for循环, we can completely no longer rely on the index to get the value of dic={' a ': 1, ' B ': 2, ' C ': 3}for K in dic:    print (dic[k]) #for循环的工作原理 # #: Dic.__iter__ of the object executing in () method to get an iterator object iter_dic#2: Execute Next (iter_dic), assign the resulting value to K, and then execute the Loop body Code # 2: Repeat the process until you catch the exception stopiteration, ending the loop

Advantages and disadvantages of iterators

#优点:  -Provides a unified, index-independent iterative approach  -lazy computing, saving memory # Disadvantages:  -Unable to get the length (only after the next finish to know how many values)-one  -time, can only go backwards, not forward



Two what is a generator

#as long as there is a yield keyword inside the function, the result of the function name () is the generator, and the function internal code is not executeddeffunc ():Print('====>first')    yield1Print('====>second')    yield2Print('====>third')    yield3Print('====>end') G=func ()Print(g)#<generator object func at 0x0000000002184360>

Generators are iterators

G.__iter__g.__next__#2, so the generator is an iterator, so you can take this value res=next (g) print (res)
process-oriented programming
#1, first emphasize: process-oriented programming is definitely not a function of programming so simple, process-oriented is a kind of programming ideas, ideas, and programming ideas are not dependent on specific language or grammar. The implication is that even if we do not rely on the function, can also be based on the process-oriented thought program # #, the process of defining the core is the process of the word, the process refers to the steps to solve the problem, that is, what to do with the process-oriented design program is like in the design of a pipeline, is a mechanical thinking mode Advantages: Complex problems flow, and then simplify # #, shortcomings: Scalability is poor, modify any stage of the pipeline, will reaching # #, application: Not high scalability scenarios, typical cases such as the Linux kernel, git,httpd#6, example pipeline 1: User input user name, Password---> user authentication---> Welcome Interface Pipeline 2: User input SQL--->sql parsing---> Execution function

Python learning fourth iterator builder for process programming

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.