Table of Contents
This updated guide examines The Iter ()() Function in Python and organizes the essential facts, background, and practical takeaways in clear American English.
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])
: 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:
Object parameterSentinel parameterDescribe Object set (set, tuple) No iterator created for object set User defined object (Custom object) None
- execute the __iter __ () and __next __ () methods. - execute __getitem __ () method with integer arguments starting at 0.
Custom object does not deploy __iter __ (), __next () or __getitem __ () None Exception ExceptionTypeErrorexception 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 exceptionStopIterationexception 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.
Theniter(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
FAQ
What is The Iter ()() Function in Python about?
It provides a structured overview of iter (), explains the main context, and highlights practical takeaways for readers.
Why does this topic matter?
Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.
How should readers use this information?
Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.
Reader Comments 0
Sign in with email or Google to join the discussion.