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.

ClassNameclassClassName()constructioninstancenewly created objectobject variablelater accessruncreatesassign to
What happens when ClassName() runs and the result is assigned to a variable?
Worked example

Construction and Later Access

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

  1. 1
    Run ClassName()

    The class call constructs an instance.

  2. 2
    Assign the result

    The constructed instance is assigned to the variable named object.

  3. 3
    Use object later

    The variable provides a way to access the constructed instance later.

Result

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.

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

Tracing the Calling Instance

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

  1. 1
    Identify the caller

    Start with the object placed before the dot in the method call.

  2. 2
    Connect the method

    Inside the method, self refers to that object instance.

  3. 3
    Use the instance connection

    The self connection allows the method to be associated with the particular instance that called it.

Result

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.

an.party()object.method()PartyAnimal.party(an)Class.method(object)anselfpartysame methoduses an aspasses an asconnects to
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 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 like

    Using ClassName() without recognizing that it constructs an instance

    Why it fails

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

    Do this instead

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

  • Common mistake

    Ignoring the object before the dot

    What it looks like
    Reading object.method() as only a method name
    Why it fails

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

    Do this instead

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

  • Common mistake

    Assuming self means the class

    What it looks like

    Interpreting self in an instance method as the class name

    Why it fails

    self refers to the object instance that called the method.

    Do this instead

    Trace the calling object and connect that object to self.

  • Common mistake

    Believing the two calling forms represent different method behavior

    What it looks like
    Treating an.party() and PartyAnimal.party(an) as unrelated calls
    Why it fails

    The source identifies both forms as equivalent.

    Do this instead

    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.
Worked example

Practice Check

Explain the instance connection in an.party() and PartyAnimal.party(an).

  1. 1
    Read the shorthand call

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

  2. 2
    Read the explicit call

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

  3. 3
    Compare self

    Because both calls use an as the object, both connect self to the same instance.

Result

The two calls are equivalent because both invoke party for the instance an.

The Complete Trace

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

Related concepts

More concepts starting with U