Back to questions
JuniorTheoryCommon
What does the program print?
class Dog:
sound = "woof"
a = Dog()
b = Dog()
a.sound = "grr"
Dog.sound = "bark"
print(a.sound, b.sound)grr bark
- Expecting
bark bark, as if changing the class reached every instance:a.sound = "grr"created an attribute ofaitself, and it hides the class one. - Expecting
b.soundto staywoof, as if each instance copied it when created:bhas nosoundof its own, so it reads the class attribute, nowbark.
- What would
del a.soundfollowed byprint(a.sound)print? - Why is a list as a class attribute risky when instances append to it?