Understanding Class Methods and Instance Methods
Object construction using ClassName() creates an instance and assigns it to a variable for later access.
From Class to Object
A class provides a way to construct objects. When ClassName() runs, it creates an instance. Assigning that expression to a variable gives you a name through which the newly created instance can be accessed later.
Construction and Later Access
Trace the role of ClassName() in the expression object = ClassName().
- 1Run ClassName()
The class call constructs an instance.
- 2Assign the result
The constructed instance is assigned to the variable named object.
- 3Use object later
The variable provides a way to access the constructed instance later.
The variable object is available for later access to the constructed instance.
Finding Data with Dot Notation
Dot notation starts with a particular object and then names the data or method to access. In the form object.attribute, the attribute is accessed through that specific object. In the form object.method(), the method is called through that specific object. The object at the left of the dot identifies which instance the operation is connected to.
| Expression form | What it accesses |
|---|---|
| object.attribute | Data belonging to a specific object instance |
| object.method() | A method called for a specific object instance |
The object before the dot identifies the instance involved in the access.
Connecting self to an Instance
The self parameter inside a method refers to the object instance that called the method. This is what links the method's work to one particular object instead of leaving the method disconnected from the instance involved in the call.
Tracing the Calling Instance
A method is called through an object. What does self identify inside that method?
- 1Identify the caller
Start with the object placed before the dot in the method call.
- 2Connect the method
Inside the method, self refers to that object instance.
- 3Use the instance connection
The self connection allows the method to be associated with the particular instance that called it.
self refers to the object instance that made the method call.
Two Ways to Call a Method
A method can be called with shorthand dot notation or with explicit class syntax. The shorthand form places the object before the dot. The explicit form names the class first and passes the object as an argument. These two forms are equivalent: in both cases, the object becomes the self argument for the method call.
What do you think happens?
Which object is connected to self in both calls: an.party() and PartyAnimal.party(an)?
Reveal answer
an
In the shorthand form, an is the object before the dot. In the explicit form, an is passed as the object argument. Both forms connect the method call to the same instance.
Class-Level Definition and Instance-Level Use
| Part | Role |
|---|---|
| Class | Provides the named method and can be used in the explicit calling form. |
| Object instance | Is the particular object involved in a method call. |
| self | Refers inside the method to the object instance that called it. |
| Dot notation | Connects a particular object instance to its data or method. |
The class supplies the method context, while the instance identifies which object the call concerns.
The important distinction is between where the method is named and which object is involved when it runs. The explicit form PartyAnimal.party(an) names the class and method directly, but an is still the instance passed to the call. The shorthand form an.party() starts with that instance and reaches the same method. In either form, the method is connected to the specific object through self.
Mistakes with Methods and Instances
Common mistake Treating ClassName() as if it only names a class
What it looks likeUsing ClassName() without recognizing that it constructs an instance
Why it failsThe call creates an instance that can be assigned to a variable for later access.
Do this insteadTrace ClassName() as a construction step, then identify the variable receiving the resulting instance.
Common mistake Ignoring the object before the dot
What it looks likeReading object.method() as only a method nameWhy it failsThe object before the dot identifies the specific instance connected to the access or call.
Do this insteadIdentify the object first, then identify the attribute or method being accessed.
Common mistake Assuming self means the class
What it looks likeInterpreting self in an instance method as the class name
Why it failsself refers to the object instance that called the method.
Do this insteadTrace the calling object and connect that object to self.
Common mistake Believing the two calling forms represent different method behavior
What it looks likeTreating an.party() and PartyAnimal.party(an) as unrelated callsWhy it failsThe source identifies both forms as equivalent.
Do this insteadMatch the object in the shorthand form with the object argument in the explicit form.
Trace the Call
For each expression, identify the object instance connected to the method call and state which value becomes self: an.party() and PartyAnimal.party(an). Then explain why the two expressions are equivalent.
Hints
- In the first expression, look to the left of the dot.
- In the second expression, look at the object supplied inside the parentheses.
- Both expressions use the same object instance.
Practice Check
Explain the instance connection in an.party() and PartyAnimal.party(an).
- 1Read the shorthand call
In
an.party(), an is the object before the dot, so the call is connected to an. - 2Read the explicit call
In
PartyAnimal.party(an), an is supplied as the object argument to the class-and-method form. - 3Compare self
Because both calls use an as the object, both connect self to the same instance.
The two calls are equivalent because both invoke party for the instance an.
The Complete Trace
- 1Run
ClassName()to construct an instance. - 2Assign the constructed instance to a variable so it can be accessed later.
- 3Use object.attribute or
object.method()to access data or behavior through a particular instance. - 4When the object calls a method, self refers to that object instance inside the method.
- 5Remember that
object.method()andClass.method(object)are equivalent calling forms.
Key Takeaways
ClassName()constructs an instance, and assigning it to a variable makes that instance available for later access.- Dot notation uses a particular object to access its data or call its methods.
- self refers to the object instance that called the method.
object.method()andClass.method(object)are equivalent ways to call a method.- To trace a method call, identify the object involved first; that object is the instance connected to self.