Concepts / Understanding Class Methods and Instance Methods

Understanding Class Methods and Instance Methods

Object construction using ClassName() creates an instance and assigns it to a variable for later access.

  • Programming

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.

runcreatesassign toClassNameclassClassName()constructioninstancenewly created objectobject variablelater access
What happens when ClassName() runs and the result is assigned to a variable?

Construction and Later Access

Trace the role of ClassName() in the expression object = ClassName().

Run ClassName(): The class call constructs an instance.

Assign the result: The constructed instance is assigned to the variable named object.

Use 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 formWhat it accesses
object.attributeData 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.

callsbecomeslinks method toobjectcalling instancemethoddefined on the classselfcalling instanceobject dataspecific instance
How does self connect a method call to the particular object instance whose data should be read or changed?

Tracing the Calling Instance

A method is called through an object. What does self identify inside that method?

Identify the caller: Start with the object placed before the dot in the method call.

Connect the method: Inside the method, self refers to that object instance.

Use 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.

uses an aspasses an asconnects toan.party()object.method()PartyAnimal.party(an)Class.method(object)anselfpartysame method
How are object.method() and Class.method(object) connected, and how does the object become self in both forms?
python

What do you think happens?

Which object is connected to self in both calls: an.party() and PartyAnimal.party(an)?

  • PartyAnimal
  • an
  • Neither object
Reveal answer

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

PartRole
ClassProvides the named method and can be used in the explicit calling form.
Object instanceIs the particular object involved in a method call.
selfRefers inside the method to the object instance that called it.
Dot notationConnects a particular object instance to its data or method.

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

  • Treating ClassName() as if it only names a class

    The call creates an instance that can be assigned to a variable for later access.

    Fix: Trace ClassName() as a construction step, then identify the variable receiving the resulting instance.

  • Ignoring the object before the dot

    The object before the dot identifies the specific instance connected to the access or call.

    Fix: Identify the object first, then identify the attribute or method being accessed.

  • Assuming self means the class

    self refers to the object instance that called the method.

    Fix: Trace the calling object and connect that object to self.

  • Believing the two calling forms represent different method behavior

    The source identifies both forms as equivalent.

    Fix: Match the object in the shorthand form with the object argument in the explicit form.

Trace the Call

EASY

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).

Read the shorthand call: In an.party(), an is the object before the dot, so the call is connected to an.

Read the explicit call: In PartyAnimal.party(an), an is supplied as the object argument to the class-and-method form.

Compare 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

  1. Run ClassName() to construct an instance.
  2. Assign the constructed instance to a variable so it can be accessed later.
  3. Use object.attribute or object.method() to access data or behavior through a particular instance.
  4. When the object calls a method, self refers to that object instance inside the method.
  5. Remember that object.method() and Class.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() and Class.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.