Code Challenge
Create two instances with independent attendee lists. Add a name to only one instance and show both sizes.
Understand the idea
Write your own solution before opening the worked answer. Predict the output from the supplied values, then compare the actual result. Check both an ordinary case and the boundary described below; a passing example is more useful when you can explain why it passes.
Where to try it
Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.
Your task
Create two instances with independent attendee lists. Add a name to only one instance and show both sizes.
Show the worked solution
class Workshop:
def __init__(self):
self.attendees = []
first, second = Workshop(), Workshop()
first.attendees.append("Mira")
print(len(first.attendees), len(second.attendees))
Check the result
The sizes are 1 and 0.
Test one more case
Add to the second instance and confirm neither change overwrites the other.