Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
Class attributes are defined on the class and shared by all instances; instance attributes are set on self, usually in __init__, and belong to one object. self is the instance the method was called on.
When you call obj.method(), Python passes obj as the first argument, which by convention is named self. Attributes assigned as self.name = value in init are stored in that instance's own namespace. Attributes defined in the class body are stored on the class and found through the instance if the instance has no attribute with the same name. Assigning obj.count = 1 creates an instance attribute that shadows the class one rather than changing it. A mutable class attribute, like a list, is shared by every instance, which is a common source of bugs.