Python · Stage 9 – Intro to Objects · Lesson 47 of 50

What Is self?

Understand how a method refers to the particular object receiving the call.

Two objects can use the same method but have different attributes. Self tells the method which object is currently involved.

class Dog:
    def __init__(self, name):
        self.name = name

    def introduce(self):
        print(self.name)
Line-by-line explanation
  1. The constructor stores a name on the current object.
  2. Introduce receives the current object as self.
  3. self.name reads that object’s own name.
first = Dog("Milo")
second = Dog("Luna")
second.introduce()
Line-by-line explanation
  1. Two separate objects are created.
  2. Each has its own name.
  3. Calling second’s method displays Luna.