Class and instance attributes

A name assigned in the class body, like species, is a class attribute. There is one copy, shared by every instance. A name set on self in __init__ is an instance attribute, one per object.

Reading rex.species looks on the instance first, then on the class. Assigning rex.species = "wolf" creates a new instance attribute that hides the class one, for rex only.

The classic bug is a mutable class attribute. tricks = [] in the class body is one list for every dog, so rex.learn("sit") teaches fido too. It's the same trap as a mutable default argument, and a favourite in Debugging interviews.

The fix: create the list in __init__ with self.tricks = [].

Run it, then apply the fix and check that fido.tricks is empty.