正在加载图片...
Back to Iterators -The For Statement for <name>in <expression>: <suite> 1.Evaluate the header <expression>,which must evaluate to an iterable object. 2.For each element in that sequence,in order: a.Bind <name>to that element in the first frame of the current environment b.Execute the <suite> When executing a for statement,iter returns an iterator and next provides each item: >>>counts=[1,2,3] >>> items iter(counts) >>> counts [1,2,3] >>> try: >>> for item in counts: while True: print(item) item next(items) 1 These are print(item) 2 equivalent! except StopIteration: 3 pass 2 Stoplteration is raised whenever next is 3 called on an empty iteratorBack to Iterators - The For Statement for <name> in <expression>: <suite> 1. Evaluate the header <expression>, which must evaluate to an iterable object. 2. For each element in that sequence, in order: a. Bind <name> to that element in the first frame of the current environment b. Execute the <suite> When executing a for statement, iter returns an iterator and next provides each item: >>> counts = [1, 2, 3] >>> for item in counts: ... print(item) 1 2 3 >>> counts = [1, 2, 3] >>> items = iter(counts) >>> try: ... while True: ... item = next(items) ... print(item) ... except StopIteration: ... pass 1 2 3 The same! These are equivalent! StopIteration is raised whenever next is called on an empty iterator
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有