About "missing 1 required positional argument: ‘self’" in Python
Tags: python, programming
Published: 2023-02-04 14:51:19

> ✋ **Update:** This post was originally published on my blog [decodingweb.dev](https://www.decodingweb.dev), where you can read the [latest version](https://www.decodingweb.dev/python-typeerror-missing-1-required-positional-argument-self) for a 💯 user experience. _~reza_

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.

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.

```python
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. <u>Instantiate the class</u>
2. <u>Use static methods</u>

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.

```python
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:

```python
# 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:

```python
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:**

- [Unindent does not match any outer indentation level error in Python (Fixed)](https://www.decodingweb.dev/python-indentationerror-unindent-does-not-match-any-outer-indentation-level-fix)
- [AttributeError module 'DateTime' has no attribute 'strptime' (Fixed)](https://www.decodingweb.dev/attributeerror-module-datetime-has-no-attribute-strptime-python)
- [AttributeError: 'str' object has no attribute 'decode' (Fixed)](https://www.decodingweb.dev/python-attributeerror-str-object-has-no-attribute-decode-fix)
- [How a computer program works](https://www.decodingweb.dev/how-a-computer-program-works)
- [How back-end web frameworks work?](https://www.decodingweb.dev/backend-web-frameworks)
- [Your master sword: a programming language](https://www.decodingweb.dev/your-master-sword-a-programming-language)