Python Programming - Classes - Discussion

Discussion Forum : Classes - General Questions (Q.No. 20)
20.
In Python, what is the purpose of the __iter__ method in a class?
It initializes an iterator for the class.
It defines the behavior of the class when used in a loop.
It is called when an object is instantiated.
It indicates the inheritance hierarchy of the class.
Answer: Option
Explanation:
The __iter__ method is used to initialize an iterator for a class. It should return an object that implements the __next__ method, allowing the class to be used in a for loop.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.