What makes something iterable in Python? ie. can loop over it with for
Is it possible for me to create an iterable class in Python? If so, how?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To make a class iterable, write an
__iter__()method that returns an iterator:prints
The example uses a list iterator, but you could also write your own iterator by either making
__iter__()a generator or by returning an instance of an iterator class that defines a__next__()method.