I'm trying to get the following code to work as intended in the comments below. For instance, I will create three Dog instances using the commands below (dogs: buddy, pascal, and kimber). However, when running buddy.teach('sit')
, it adds the 'sit' trick to both buddy and pascal instances. Can you please manipulate the code so that the attribute is defined for the instance objects rather than the class object?
Please only edit the code (do not change the commands in the comments below).
class Dog:
def __init__(self, name, tricks=set()):
self.name = name
self.tricks = tricks
def teach(self, trick):
self.tricks.add(trick)
# Change the broken code above so that the following lines work:
#
# buddy = Dog('Buddy')
# pascal = Dog('Pascal')
# kimber = Dog('Kimber', tricks={'lie down', 'shake'})
# buddy.teach('sit')
# pascal.teach('fetch')
# buddy.teach('roll over')
# kimber.teach('fetch')
# print(buddy.tricks) # {'sit', 'roll over'}
# print(pascal.tricks) # {'fetch'}
# print(kimber.tricks) # {'lie down', 'shake', 'fetch'}