Python built-in ITER function detailed introduction

Source: Internet
Author: User
English documents:

ITER (object[, Sentinel])

Return an Iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, object must be a collection object which supports the iteration protocol (the ITER () method), O R it must support the sequence protocol (the GetItem () method with an integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If The second argument, Sentinel, is given and then object must be a callable object. The iterator created in this case would call object with the no arguments for each call to its next () method; If the value returned is equal to Sentinel, Stopiteration would be raised, otherwise the value would be returned.

One useful application of the second form of ITER () is to read lines of a file until a certain line is reached. The following example reads a file until the ReadLine () method returns an empty string:

With open (' mydata.txt ') as FP:    for Line in ITER (Fp.readline, "):        process_line (line)

Description

1. Function function returns an object that can be iterated.

2. When the second parameter is not provided, the first argument must be a set (dictionary, collection, immutable collection) that supports an iterative protocol (that is, the ITER () method is implemented), or a sequence that supports the sequence protocol (that is, the GetItem () method is implemented, and the method receives an integer parameter starting from 0) (tuple, list, string), or it will be an error.

>>> A = iter ({' A ': 1, ' B ': 2}) #字典集合 >>> A<dict_keyiterator object at 0x03fb8a50>>>> next ( A) ' A ' >>> next (a) ' B ' >>> Next (a) Traceback (most recent):  File "<pyshell#36>", line 1, in <module>    next (a) stopiteration >>> a = iter (' ABCD ') #字符串序列 >>> A<str_iterator Object At 0x03fb4fb0>>>> next (a) ' A ' >>> next (a) ' B ' >>> next (a) ' C ' >>> next (a) ' d ' > >> Next (a) Traceback (most recent call last):  File "<pyshell#29>", line 1, in <module>    next (a) Stopiteration

3. When Sentinel provides the second parameter, the first argument must be a callable object. Creates an iterative object that invokes the callable object when the next method is called, and throws an Stopiteration exception when the return value and the Sentinel value are equal, terminating the iteration.

# definition class >>> class Itertest:def __init__ (self): Self.start = 0 Self.end = Ten def get_next_value (self): current = Self.start if current < self.end:self. Start + = 1 else:raise stopiteration return current>>> itertest = itertest () #实例化类 >&G T;> a = iter (itertest.get_next_value,4) # Itertest.get_next_value is a callable object, Sentinel value is 4>>> a<callable_ Iterator object at 0x03078d30>>>> next (a) >>> next (a) >>> next (a) >>> next (a) > >> Next (a) #迭代到4终止Traceback (most recent): File "<pyshell#22>", line 1, in <module> next (a ) Stopiteration 

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.