TypeError: missing 1 required positional argument: ‘self’ (Fixed)

Updated Oct 23, 2023 ⤳ 3 min read

The Python “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.

🎧 Debugging Jam

Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat – subscribe for the ultimate coding groove!" Let the bug-hunting begin! 🎵💻🚀

24/7 lofi music radio banner, showing a young man working at his computer on a rainy autmn night with hot drink on the desk.

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, C++, etc.

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.

1) 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 function (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())

2) 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.

Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.

`