Python built-in function (--iter)

Source: Internet
Author: User

English documents:

iter(object[, Sentinel])

Return aniteratorObject. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument,ObjectMust be a collection object which supports the iteration protocol (the__iter__()method), or it must support the sequence protocol (the__getitem__()Method with an integer arguments starting at0). If it does not support either of those protocols,TypeErroris raised. If the second argument,Sentinel, is given, thenObjectMust be a callable object. The iterator created in this case would callObjectWith no arguments for each call to its__next__()Method If the value returned is equal toSentinel,StopIterationWould be raised, otherwise the value would be returned.

One useful application of the second form of are to iter() 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 in'):        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 collection (dictionary, collection, immutable collection) that supports an iterative protocol (that is, the __iter__ () method is implemented), or a sequence protocol (that is, the __getitem__ () method is implemented, and the method receives an integer parameter starting from 0) Sequence (tuple, list, string), otherwise an error will be expected.

>>> A = iter ({'A': 1,'B': 2})#Dictionary Collection>>>a<dict_keyiterator Object at 0x03fb8a50>>>>Next (a)'A'>>>Next (a)'B'>>>Next (a) Traceback (most recent): File"<pyshell#36>", Line 1,inch<module>Next (a) Stopiteration>>> A = ITER ('ABCD')#string Sequence>>>a<str_iterator Object at 0x03fb4fb0>>>>Next (a)'a'>>>Next (a)'b'>>>Next (a)'C'>>>Next (a)'D'>>>Next (a) Traceback (most recent): File"<pyshell#29>", Line 1,inch<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 when the return value and the Sentinel value are equalStopIteration异常, 终止迭代。

#Defining Classes>>>classitertest:def __init__(self): Self.start=0 Self.end= 10defGet_next_value (self): current=Self.startifCurrent <Self.end:self.start+ = 1Else:            Raisestopiterationreturn Current>>> itertest = Itertest ()#Instantiating Classes>>> a = iter (itertest.get_next_value,4)#Itertest.get_next_value is a callable object with a Sentinel value of 4>>>a<callable_iterator Object at 0x03078d30>>>>Next (a) 0>>>Next (a)1>>>Next (a)2>>>Next (a)3>>> Next (a)#iteration to 4 terminationTraceback (most recent): File"<pyshell#22>", Line 1,inch<module>Next (a) Stopiteration

Python built-in function (--iter)

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.