Concepts / Calling Methods on Objects

Calling Methods on Objects

The self parameter is the first parameter in every class method and refers to the instance itself.

  • Programming

The Object Behind the Call

When you call a method through an object, Python connects that method call to the particular instance on the left side of the dot. The first parameter in the method receives that instance. By convention, this parameter is named self.

myobjectcalling instanceMyClass.methodclass methodselfthe instancecallsbecomes
How is self connected to the particular object whose method was called?

Tracing the Automatic Argument

Consider the generated call myobject.method(arg1, arg2). Python automatically supplies myobject as the method's first argument. You write the object before the dot, but you do not write self in the call. The object is passed into the first parameter, which is conventionally named self.

myobjectinstanceselffirst parameterarg1remaining argumentparameter1next parameterautomatic passingordinary argument passing
What data moves from the object at the left of the dot into the method's first parameter?
Worked example

Following the First Argument

Trace the generated call for myobject.method(arg1, arg2).

  1. 1
    Start with the instance call

    The call begins with myobject before the dot, followed by method and the arguments arg1 and arg2.

  2. 2
    Place the instance first

    Python uses myobject as the first argument supplied to the method.

  3. 3
    Keep the remaining arguments in order

    arg1 and arg2 remain after the instance.

  4. 4
    Read the class-method form

    The equivalent form is MyClass.method(myobject, arg1, arg2).

Result

The object before the dot becomes the first argument in the class-method form.

Callerstarts the callPythonpasses the instanceMyClass.methodreceives argumentsmyobject.method(arg1, arg2)MyClass.method(myobject, arg1, arg2)
How does myobject.method(arg1, arg2) become MyClass.method(myobject, arg1, arg2)?

Why the First Position Matters

self represents the instance itself. It occupies the first parameter position in a class method, while the arguments you supply in the call follow it. This position lets Python associate the method call with the particular object that appeared before the dot.

Position 1myobject → selfPosition 2arg1Position 3arg2thenthen
How do the instance and the remaining arguments map to the method's parameter positions?

The Name self

Python allows other parameter names technically, but self is the established convention for the first parameter of a class method. Using self communicates to readers that this parameter refers to the instance itself. The convention makes the method's parameter positions easier to recognize when reading class definitions.

Parameter nameTechnical statusMeaning communicated to readers
selfConventionalThis is the instance itself
another nameTechnically allowedLess clearly communicates the standard instance parameter

The conventional name self is strongly preferred even though another name is technically allowed.

Mistakes with Method Calls

  • Common mistake

    Treating self as an argument that must be written in the ordinary object call.

    What it looks like

    Trying to add self explicitly when calling myobject.method(arg1).

    Why it fails

    Python automatically passes the instance to the method.

    Do this instead

    Call the method through the object without explicitly supplying self.

  • Common mistake

    Forgetting that the instance occupies the first parameter position.

    What it looks like

    Reading myobject.method(arg1, arg2) as though arg1 were the first value received by the method.

    Why it fails

    The object before the dot is passed first, and the remaining arguments follow it.

    Do this instead
    Trace the equivalent form MyClass.method(myobject, arg1, arg2).
  • Common mistake

    Replacing self with a different parameter name without a strong reason.

    What it looks like

    Using another name for the first parameter and expecting readers to recognize the instance immediately.

    Why it fails

    Other names are technically allowed but strongly discouraged because self is the convention.

    Do this instead

    Use self as the first parameter name in class methods.

Practice the Transformation

EASY

A method is called as device.activate(mode). Identify the instance that Python passes automatically, then describe the equivalent class-method call using the class name Device.

Hints
  • The instance is the object before the dot.
  • Place that instance before mode in the class-method form.
  • Use the pattern Class.method(instance, remaining arguments).

What do you think happens?

What is the equivalent class-method form of device.activate(mode)?

  • Device.activate(mode)

  • Device.activate(device, mode)

  • device.Device.activate(mode)

Reveal answer
Answer:

Device.activate(device, mode)

The object before the dot, device, is automatically passed as the first argument. The explicitly supplied argument mode follows it.

Key Takeaways

  1. 1
    self is the first parameter in every class method and refers to the instance itself.
  2. 2
    Python automatically passes the instance when you call a method through an object.
  3. 3
    The instance method call myobject.method(arg1, arg2) corresponds to MyClass.method(myobject, arg1, arg2).
  4. 4
    The instance occupies the first parameter position, before the other arguments.
  5. 5
    Use self by convention; although other names are technically allowed, they are strongly discouraged.

At a glance

  • self represents the particular instance whose method is being called.
  • Python supplies that instance automatically as the method's first argument.
  • An instance call can be understood as a class-method call with the instance inserted first.
  • The name self is a strong convention that makes the instance parameter clear to readers.

Related concepts

More concepts starting with C