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
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.
- 1Construct
Use
PartyAnimal()as the construction expression. The class-name call creates an instance. - 2Assign
Associate the created instance with the variable an. The variable can now be used to access that specific object.
- 3Continue
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().
- 1Before the dot
an identifies the particular object whose behavior is being requested.
- 2After the dot
party identifies the method associated with that object.
- 3Parentheses
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.
- 1Select the instance
The name an identifies the object before the dot.
- 2Select the method
The name party identifies the method after the dot.
- 3Connect the method
Within that method call, self refers to the instance identified by an.
- 4Reach 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 |
The two forms use different notation but connect the same method to the same instance.
Mistakes with Instance Context
Common mistake Treating ClassName() as the object variable
What it looks likeAfter construction, confusing PartyAnimal with an
Why it failsPartyAnimal is the class name used in the construction expression, while an is the variable assigned to the newly created instance.
Do this insteadTrack the sequence: ClassName() creates an instance, and assignment gives that instance a variable name.
Common mistake Ignoring the object before the dot
What it looks likeReading an.party() as if party were selected without reference to an
Why it failsDot notation accesses data or methods belonging to the specific object placed before the dot.
Do this insteadIdentify the object first, then identify the attribute or method after the dot.
Common mistake Treating self as an unrelated, general object
What it looks likeAssuming self always refers to one fixed instance
Why it failsself refers to the object instance that called the method.
Do this insteadFor each call, ask which object supplied the method call. That object is the instance linked through self.
Common mistake Assuming the two method-call forms select different behavior
What it looks likeTreating an.party() and PartyAnimal.party(an) as unrelated callsWhy it failsThe source identifies both forms as equivalent.
Do this insteadCompare 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.
- 1player
player is the variable assigned to the instance created by
MusicPlayer(). - 2player.volume
The expression uses dot notation to access volume as data belonging to the instance represented by player.
- 3player.play()
The expression uses dot notation to call play for the instance represented by player.
- 4self
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
- 1
ClassName()creates an instance, and assignment gives that instance a variable for later access. - 2Dot notation uses the object before the dot to select that object's data or method.
- 3self refers to the instance that called the method, linking the method to that instance's data.
- 4
object.method()andClass.method(object)are equivalent ways to call a method for the same object. - 5When tracing an object-oriented expression, identify the instance first, then the requested attribute or method.
At a glance
- 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.