LearnFrenzy
Sign in Take a free test

OOP in Python · Predict the Output · Question 3

A mutable class attribute Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

What is the output of the following Python program?

main.py
class Team:
    members = []

    def add(self, name):
        self.members.append(name)

t1, t2 = Team(), Team()
t1.add("Asha")
t2.add("Ravi")
print(t1.members, t2.members is t1.members)
  • A ['Asha'] False
  • B ['Asha'] True
  • C ['Ravi'] False
  • D ['Asha', 'Ravi'] True Correct
Correct Answer: Option D

Both objects mutate the single list stored on the class.

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