About "missing 1 required positional argument: ‘self’" in Python

About "missing 1 required positional argument: ‘self’" in Python

The Python error TypeError: missing 1 required positional argument: ‘self’ usually occurs if you call a method directly on a class – rather than an instance of that class.

Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza

Here’s what the error looks like:

Traceback (most recent call last):
 File /dwd/sandbox/test.py, line 9, in <module>
  print(movie.get_title())
     ^^^^^^^^^^^^^^^^^
TypeError: Movie.get_title() missing 1 required positional argument: self

When you call a method on a Python object, a parameter (conventionally named self) is implicitly passed to it as its first argument.

The self parameter represents the object's state and is equivalent to the this keyword in JavaScript, PHP, and C++.

That said, you should always reserve the first parameter of your non-static methods for the object instance.

class Movie:
    # self defined as the first parameter for the constructor 
    def __init__ (self, name):
        self.name = name

    def get_title(self):
        return self.name

Unlike keyword arguments, the order of positional arguments matters in Python, and you'll have to pass them to the respective method in the order they are defined.

If you call the method get_title() directly on the class Movie, the object's instance (self) isn't passed to it. And since it expects one, Python will throw the "TypeError: missing 1 required positional argument: 'self'".

To fix it, instantiate the class and call your method on the instantiated object. And the error would go away.

Alternatively, you can use static methods if you don't need the self parameter.

Let's get a little bit deeper.

How to fix missing 1 required positional argument: 'self'

As mentioned earlier, there are two options to fix the error:

  1. Instantiate the class

  2. Use static methods

Let's explore each approach with examples.

Instantiate the class: The self parameter is only passed to methods if called on an object. That said, you can fix the error by instantiating the class and calling the method on the object instance.

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

    def get_title(self):
        return self.name

movie = Movie()
print(movie.get_title())

Or without storing the object's instance in a variable:

# Instantiating the class without storing the instance
print(Movie().get_title())

Use static methods: If there's no reference to the self parameter in your method, you can make it static. As a result, you'll be able to call it directly on the class:

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

    @staticmethod
    def play_movie():
        return 'playing ...'

print(Movie.play_movie())

Please note you need to remove self in the method definition as well. Otherwise, the method keeps expecting the self parameter.

And that's how you fix the this type error! I hope this quick guide fixed your problem.

Thanks for reading.


❤️ You might like: