HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

2) Classes, Objects, and Methods Lesson

What is Python self?

6 min to complete · By Martin Breuss

You've used the self parameter when creating any new methods in your custom classes, but you might have wondered what exactly it does, why you need it, and why you don't need to pass it as an argument when calling a method.

Python Class Self Awareness

Python class instances are self-aware! When you reference self within a class definition, it means that you're referencing the instance object itself.

This means that when you pass self to a method, you can access all the instance variables and other methods that you defined on the object. It allows you to keep the connection with your instance.

That means, for example, that the reference self.name will be accessible inside of every method that takes self as its first parameter.

Example Python Self

Check out the code below for the self parameter in action.

class Ingredient:

    """Models a food item used as an ingredient."""
    def __init__(self, name):
        self.name = name

    def expire(self):
        """Expires the ingredient."""
        print(f"whoops, these {self.name} went bad...")
        self.name = "expired " + self.name

As you can see in the code snippet above, both methods of your Ingredient() class, __init__() and expire(), take self as their first parameter. This gives them access to the whole namespace of your instance:

  • __init__() can assign the value of name that it receives during instantiation to the object through self.name = name.
  • expire() can access and change the value of self.name with the references to it in its method body.

If either of the methods wouldn't take self as a parameter, this would not be possible.

Illustration of a lighthouse

Tasks

  • Define a new method, selfless(), that doesn't take self as a parameter.
  • Now try to call this method on a new object. What do you get?

Type Error with Python Self

You'll see that you can define a method without the self parameter. However, when you try to call such a method on your object, you'll receive an error message:

i.selfless()
# OUTPUT:
# TypeError: selfless() takes 0 positional arguments, but 1 was given

This message might seem unintuitive at first, but it gives you a great hint for why you never had to pass an argument for self when calling any of your other methods.

The reference to your object instance, represented by self, gets passed implicitly to your methods. More specifically, you're passing the reference through the object that you're calling the method from:

i = Ingredient("peas")
i.expire()

Type Error Solution

In your class definition, you wrote that expire() takes exactly one argument, self. However, in the code snippet above, you seemingly call expire() without any arguments. The reason for this is that you're passing the reference to your object instance, i, by calling the method on it.

When you compare this to a function, you can think that i.expire() is equivalent to expire(i), where i points to your Ingredient() object that inside of your class definition gets referenced through self.

Colorful illustration of a light bulb

Info: Like many things in Python, the name for self is just a standard rather than a keyword. You could use any other variable name instead. However, just like with most things where there are standards, it absolutely makes sense to stick with self as the name for the reference to your instance.

After exploring deeper into how to define methods, what self stands for, and why you need it, you're ready to answer a question that you might have been thinking about for a while:

What's the difference between functions and methods?

A short rule-of-thumb answer to this question is that methods are functions that are part of a class.

Colorful illustration of a light bulb

Additional Resources

Summary: What is Python Self

  • Instance methods take the instance of the object that they are part of as their first argument through the self parameter
  • The reference to the instance object gets passed implicitly when calling the method on the instance.