Two players share a class variable, and one of them gets its own value with setattr.
class Player:
team = "red"
def __init__(self, name):
self.name = name
def __str__(self):
return f"{self.name}/{self.team}"
a = Player("Ana")
b = Player("Ben")
setattr(b, "team", "blue")
Player.team = "green"
print(a, b, len(vars(a)), len(vars(b)))
What does the program print?
Log in to join the discussion.
Loading…