Concepts / Class Definition and Instantiation

Class Definition and Instantiation

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

  • Programming

The Hidden First Argument

When you call a method through an object, Python supplies that object to the method automatically. The method receives the object through its first parameter. By convention, that parameter is named self.

Key point

self refers to the particular instance on which the method was called. It is not an extra value that you normally type in the method call.

Following the Call

Consider the instance method call myobject.method(arg1, arg2). Python treats the call as the class method call MyClass.method(myobject, arg1, arg2). The instance moves into the first parameter position, while the other arguments keep their order.

myobjectPythonMyClass.methodmethod(arg1, arg2)myobject, arg1, arg2
What happens when an instance method call is transformed into a class method call?
Worked example

Reading the First Argument

Trace the transformation of myobject.method(arg1, arg2).

  1. 1
    Start with the instance call

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

  2. 2
    Move the instance into the first position

    Python supplies myobject as the first argument to the method.

  3. 3
    Preserve the remaining arguments

    arg1 and arg2 remain after the instance, producing the class method form MyClass.method(myobject, arg1, arg2).

Result

The first method parameter receives myobject. When that parameter follows the naming convention, it is called self.

What self Identifies

A class contains the method definition, while an instance is the particular object involved in a method call. When the method is called through that instance, the instance becomes the value received by self. Therefore, self identifies the object participating in this specific call, rather than some separate object created by the method.

MyClassmethod definitionmyobjectinstanceselfmyobjectcreatessupplies value
Which object does self refer to when a method is called on a particular instance?

The First Parameter Position

The important requirement is the first parameter position in the method definition: that position receives the instance. Python does not require the parameter to be spelled self, but self is the established convention and should always be used. Other names are technically allowed, yet strongly discouraged because they make the method less immediately recognizable to other Python programmers.

myobject.methodinstance callmyobjectinstanceselffirst parameteridentifiespassed automatically
How does Python move the instance into the method as the first argument?
Key point

The name self communicates the role of the first parameter, but the position is what allows it to receive the instance in the transformation.

Mistakes with self

  • Common mistake

    Trying to pass self explicitly in an instance method call.

    What it looks like

    Writing a call as though the instance must be supplied again by the caller.

    Why it fails

    Python automatically passes the instance when the method is called through that instance.

    Do this instead

    Call the method with only the ordinary method arguments; Python supplies the instance as the first argument.

  • Common mistake

    Thinking self refers to the class in every situation.

    What it looks like

    Treating self as the class definition rather than the object used for the call.

    Why it fails

    self refers to the instance itself.

    Do this instead

    Identify the object before the dot in the instance call. That object is the value received by self.

  • Common mistake

    Using a different name for the first parameter without a strong reason.

    What it looks like

    Replacing self with another parameter name.

    Why it fails

    Other names are technically allowed, but the convention makes the method's instance parameter clear to readers.

    Do this instead

    Use self consistently as the first parameter name.

Best practice

Use self as the first parameter of every class method. This follows the convention, makes the instance connection visible, and helps readers understand the method call transformation.

Trace It Yourself

EASY

A method is called as object_a.process(value). Describe the equivalent class method form. Then identify which value becomes self and explain whether the caller should type self explicitly.

Hints
  • Place the instance as the first argument in the class method form.
  • The instance is the object before the dot.
  • The caller does not pass self explicitly.

What do you think happens?

What is the equivalent class method form of object_a.process(value)?

  • MyClass.process(object_a, value)

  • MyClass.process(value, object_a)

  • MyClass.process(self, object_a, value)

Reveal answer
Answer:

MyClass.process(object_a, value)

Python places the instance object_a first, followed by the ordinary argument value. The caller does not add self separately.

Key Takeaways

  1. 1
    self is the first parameter in a class method and refers to the instance itself.
  2. 2
    When a method is called through an instance, Python automatically passes that instance as the first argument.
  3. 3
    The instance call myobject.method(arg1, arg2) corresponds to MyClass.method(myobject, arg1, arg2).
  4. 4
    The name self is a convention rather than the only technically permitted name, but it should be used.
  5. 5
    The caller supplies ordinary method arguments, not self explicitly.

At a glance

  • self identifies the particular instance involved in a method call.
  • Python automatically passes that instance into the method's first parameter.
  • An instance method call can be understood as a class method call with the instance inserted first.
  • self is the strongly recommended conventional name for that first parameter.
  • A caller never passes self explicitly when calling the method through an instance.

More concepts starting with C