Instance Attributes and Methods
An attribute represents data associated with the object, while a method represents behavior that can be used with the object.
From Object to Behavior
An object is a constructed instance of a class. It contains the attributes and methods defined by that class. An attribute represents data associated with the object, while a method represents behavior that can be used with the object. Instance attributes and methods work together: the attributes hold information for one object, and the methods can access that object's information.
Object: A constructed instance of a class. An object contains the attributes and methods defined by the class. The terms object and instance are sometimes used interchangeably.
Following self
Instance methods have a special first parameter that is conventionally named self. This parameter gives the method access to the particular object instance involved in the call. Through self, the method can reach attributes on that object and can call methods associated with that object.
Tracing one object's method
Imagine an object with an instance attribute named score and an instance method named add_point. When add_point runs, which object should self identify?
- 1Identify the object
The method is being used with one particular object instance.
- 2Identify self
The conventional first parameter self refers to that particular object instance.
- 3Reach the attribute
Because self gives access to the object, the method can use the object's score attribute.
- 4Change the object's data
The method can use self to set or update an attribute belonging to that same object.
self connects the running method to the specific object whose attributes and methods it should use.
Reading and Changing State
An instance attribute belongs to an individual object instance. An instance method can use self to access that attribute, read its current value, or set a new value. This is the central relationship between attributes and methods: the object's data is stored with the object, and the object's methods use the current instance to work with that data.
Using Dot Notation
Dot notation is the way an object is used to reach its attributes and methods. The object appears first, followed by a dot and the attribute or method name. When dot notation is used to call an instance method, that call identifies the particular object instance for the method. Inside the method, the conventional self parameter provides access to that instance.
Separating attribute access from method access
Consider an object named fan with an attribute named team and a method named cheer. What does each dot-notation use target?
- 1Attribute access
The form fan.team uses the object fan to reach its team attribute.
- 2Method access
The form
fan.cheer()uses the object fan to reach and call its cheer method. - 3Instance connection
When the method runs, self identifies fan as the current object instance.
The object before the dot determines which instance's attribute or method is being used.
Mistakes with Instance Members
Common mistake Treating self as an ordinary unrelated parameter.
What it looks likeA method uses self but the learner does not connect it to the object that made the call.
Why it failsThe first parameter is special because it gives the method access to the object instance involved in the operation.
Do this insteadWhen tracing a method call, identify the object before the dot and treat that object as the instance available through self.
Common mistake Confusing an object's attribute with an object's method.
What it looks likeA learner describes the object's stored data and its behavior as if they were the same kind of member.
Why it failsObjects contain both attributes and methods, but attributes represent object data while methods provide behavior.
Do this insteadAsk whether the name identifies data associated with the object or behavior that can be used with the object.
Common mistake Ignoring the object before the dot.
What it looks likeA learner focuses on a method name but does not track which object is using it.
Why it failsDot notation uses the object to reach its attributes and methods, and the method's self parameter identifies that particular instance.
Do this insteadRead a dot-notation expression from left to right: identify the object, then identify the attribute or method reached through it.
When explaining an instance method, always name the current object explicitly. A reliable explanation follows this pattern: the object is selected, the method is called through dot notation, self identifies that object inside the method, and the method uses self to access or set the object's attributes.
Practice the Trace
An object named player has an instance attribute named points and an instance method named earn. Trace the relationship in words: Which object does self identify when player uses earn, and what can the method reach through self?
Hints
- Start with the object written before the dot.
- Remember the conventional role of the first parameter in an instance method.
- The method can use self to access attributes and call methods belonging to that object.
What do you think happens?
If the object named player calls its earn method, which object does self identify inside that method?
Reveal answer
The player object
The first parameter conventionally named self gives the method access to the particular object instance involved in the call.
Key Takeaways
- 1An object is a constructed instance of a class and contains attributes and methods defined by that class.
- 2Instance attributes store data associated with an object instance.
- 3Instance methods provide behavior that can work with the object's data.
- 4The first parameter of an instance method is conventionally named self and gives access to the current object instance.
- 5Dot notation uses an object to reach its attributes and methods; when a method is called, that object is the instance available through self.
At a glance
- Objects combine attributes, which represent data, with methods, which represent behavior.
- The conventional self parameter connects an instance method to the particular object involved in a call.
- Methods use self to access or set attributes and to call methods belonging to the same object.
- Dot notation identifies the object first and then reaches one of that object's attributes or methods.