LearnFrenzy
Sign in Take a free test

Iterators and Generators · Predict the Output · Question 6

An iterable class whose __iter__ yields Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

What is the output of the following Python program?

main.py
class Team:
    def __init__(self, *names):
        self.names = names
    def __iter__(self):
        for n in self.names:
            yield n.upper()

t = Team("asha", "ravi")
print(list(t), list(t), "RAVI" in t)
  • A ['ASHA', 'RAVI'] [] True
  • B ['ASHA', 'RAVI'] ['ASHA', 'RAVI'] True Correct
  • C ['asha', 'ravi'] ['asha', 'ravi'] False
  • D ['ASHA', 'RAVI'] [] False
Correct Answer: Option B

Each loop calls __iter__ again, which creates a new generator – so the object can be iterated any number of times. in also iterates it.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links