Defining Your First Class
Object construction using ClassName() creates an instance and assigns it to a variable for later access.
From Class to Object
A class gives you a named way to work with a kind of object. Object construction begins when you write the class name followed by parentheses, as in ClassName(). This creates an instance. Assigning that expression to a variable gives you a name through which you can access the new object later.
What do you think happens?
Suppose PartyAnimal() is used to construct an object and the result is assigned to a variable named an. What does an represent afterward?
Reveal answer
Answer: A newly created instance
ClassName() creates an instance. The assignment gives that instance the variable name an, so later expressions can access the object through an.
Construction and Assignment
Naming a Constructed Object
Use the class name PartyAnimal to construct an object and make it available through the variable an.
Construct: Use PartyAnimal() as the construction expression. The class-name call creates an instance.
Assign: Associate the created instance with the variable an. The variable can now be used to access that specific object.
Continue: Expressions beginning with an can refer to data or methods belonging to this instance.
The variable an provides access to the instance created from PartyAnimal().
The important distinction is between the class name and the instance variable. PartyAnimal is used as the class in the construction expression, while an is the variable used afterward to reach the particular instance that was created.
Finding Data with Dot Notation
Dot notation places an object before a dot and the requested attribute or method after it. The object before the dot tells Python which instance is involved. Therefore, object.attribute refers to data belonging to that specific object, while object.method() requests behavior through that same object.
Reading the Expression
Interpret the two parts of an.party().
Before the dot: an identifies the particular object whose behavior is being requested.
After the dot: party identifies the method associated with that object.
Parentheses: The parentheses indicate that the method is being called.
an.party() calls the party method for the instance represented by an.
Connecting Methods with self
Inside a method, the self parameter refers to the object instance that called the method. This is what connects the method's work to one specific object's data. When a method uses self, it is working with the instance associated with the current call rather than with an unspecified object.
For a call made through an, self refers to the instance represented by an. If the same method is called through a different object, self refers to that different calling instance instead. The method name can stay the same while self determines which object's data is connected to the method.
Following self Through a Call
Explain what self represents when an.party() is called.
Select the instance: The name an identifies the object before the dot.
Select the method: The name party identifies the method after the dot.
Connect the method: Within that method call, self refers to the instance identified by an.
Reach the instance data: The method can use self to work with data belonging to that particular instance.
In an.party(), self is the instance represented by an.
Two Ways to Call a Method
A method can be called with shorthand dot notation or with explicit class syntax. The shorthand form is an.party(). The explicit form is PartyAnimal.party(an). Both forms call the party method for the same object. In the explicit form, the object is written as an argument to the class's method reference; in the dot form, the object before the dot supplies that method context.
| Calling form | Object shown | Method selected | self refers to |
|---|---|---|---|
| an.party() | an before the dot | party after the dot | the instance represented by an |
| PartyAnimal.party(an) | an inside the parentheses | party after the class name | the instance represented by an |
Mistakes with Instance Context
Treating ClassName() as the object variable
PartyAnimal is the class name used in the construction expression, while an is the variable assigned to the newly created instance.
Fix:
Track the sequence: ClassName() creates an instance, and assignment gives that instance a variable name.Ignoring the object before the dot
Dot notation accesses data or methods belonging to the specific object placed before the dot.
Fix:
Identify the object first, then identify the attribute or method after the dot.Treating self as an unrelated, general object
self refers to the object instance that called the method.
Fix:
For each call, ask which object supplied the method call. That object is the instance linked through self.Assuming the two method-call forms select different behavior
The source identifies both forms as equivalent.
Fix:
Compare the selected method and the instance supplied to it. Both use party with the instance represented by an.
Practice the Trace
A class named MusicPlayer is used to construct an object assigned to the variable player. Explain what player represents, what player.volume refers to, what player.play() requests, and what self represents while play is being called through player.
Hints
- Start with the effect of ClassName() followed by assignment.
- Use the name before the dot to identify the instance.
- For self, identify the object that made the method call.
- You can describe the relationship without writing a complete class definition.
Checking the Answer
Explain the object and method relationships in the MusicPlayer example.
player: player is the variable assigned to the instance created by MusicPlayer().
player.volume: The expression uses dot notation to access volume as data belonging to the instance represented by player.
player.play(): The expression uses dot notation to call play for the instance represented by player.
self: During that call, self refers to the instance represented by player.
The variable identifies the instance, dot notation selects its data or method, and self links the method to that same instance.
Key Takeaways
- ClassName() creates an instance, and assignment gives that instance a variable for later access.
- Dot notation uses the object before the dot to select that object's data or method.
- self refers to the instance that called the method, linking the method to that instance's data.
- object.method() and Class.method(object) are equivalent ways to call a method for the same object.
- When tracing an object-oriented expression, identify the instance first, then the requested attribute or method.
Key Takeaways
- Use ClassName() to construct an instance and assign it to a variable.
- Use dot notation to access data or methods belonging to a specific instance.
- The self parameter connects a method to the instance that called it.
- The shorthand and explicit method-call forms are equivalent when they use the same method and object.