Question 6
Back to Exercise
What is the output of the following Python program?
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!