The iter () function in Python
Continuing with the topic of Python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. Invites you to read the track.
The iter () function is built into Python, returning an iterator of the given object.
The object created from iter () can retrieve each of its elements at a given time, very useful when combined with for, while loops.
Syntax of iter () function in Python:
iter(object[, sentinel])
Read more: Iterator objects in Python
The parameters of iter function ()
The iter () function has 2 parameters:
object
: object wants to create iterator, can be set, tuples .sentinel
: special value used to represent the end of the string.
Depending on the parameter passed, iter () will have different properties as follows:
- execute the __iter __ () and __next __ () methods.
- execute __getitem __ () method with integer arguments starting at 0.
TypeError
exception Callable object Provided Function returns an iterator object that can call an object There is no argument for each call to its __next __ () method, if Sentinel is found, the exception StopIteration
exception will be generated. The value returned from iter ()
The iter () function returns an iterator object for the parameter to be passed , which can iterate through each of its elements at a given time.
In case the second parameter sentinel is passed, the function that returns the iterator object can call the callable object until no sentinel character is found.
Example 1: How does iter () work?
# danh sach nguyen am # viet boi TipsMake.com nguyenam = ['a', 'e', 'i', 'o', 'u'] nguyenamIter = iter(nguyenam) # in ra 'a' print(next(nguyenamIter)) # in ra 'e' print(next(nguyenamIter)) # in ra 'i' print(next(nguyenamIter)) # in ra 'o' print(next(nguyenamIter)) # in ra 'u' print(next(nguyenamIter))
When you run the program, the output output will be:
aeiou
Example 2: iter () works with a custom object
class PrintNumber: def __init__(self, max): self.max = max def __iter__(self): self.num = 0 return self def __next__(self): if(self.num >= self.max): raise StopIteration self.num += 1 return self.num printNum = PrintNumber(3) printNumIter = iter(printNum) # in ra '1' print(next(printNumIter)) # in ra '2' print(next(printNumIter)) # in ra '3' print(next(printNumIter)) # sinh ra StopIteration print(next(printNumIter))
Run the program, the result is:
1 2 3 StopIteration
Example 3: iter () works with callable object with sentinel
with open('mydata.txt') as fp: for line in iter(fp.readline, ''): processLine(line)
When you run the program, Python will open mydata.txt in read mode.
Then iter(fp.readline, '')
in the for loop calls readline (read each line in the text file) until the sentinel character ''
(empty string) is found.
See also: Built-in Python functions
You should read it
- Int () function in Python
- The function id () in Python
- Max () function in Python
- Zip () function in Python
- The function dir () in Python
- The ord () function in Python
- The function set () in Python
- Exec () function in Python
- The map () function in Python
- The globals () function in Python
- The next () function in Python
- Getattr () function in Python