. Advertisement .
. Advertisement .
This article is for the purpose of helping you out of the situation where TypeError: missing 1 required positional argument: ‘self’ ends up being a daunting task. Read on to learn more!
What Causes The Missing 1 Required Positional Argument: ‘self’ Error To Happen?
Before calling any of the methods specified inside a class in Python, an object must first be called, or initialized for that class.
This bugging occurs because every method in a Python class has a fixed parameter value called ‘self’ (conventional name), which stands for the object calling the function.
As a result, when trying to use a class method without having first created a class object, you get an error message that reads, “TypeError: missing 1 required positional argument: ‘self’.”
What do you think will happen if you have constructed your own class and attempt to call one of its methods? Take the following class, for instance:
class Poo: def tell_story(self): print("Good night") Poo.tell_story()
As you can see, tell_story is the only method in the class Poo. Self, a Python argument that is equivalent to this in other languages, is the argument that this method accepts.
Although you don’t have to explicitly define the ‘this’ object as a parameter to the class methods in most other languages because it is already implicit. Python simply had to be unique.
You can see that we attempt to call the tell_story function on the Poo class directly below the class definition above, which is similar to invoking a static method (so-called class method).
This results in the output that is shown below:
Traceback (most recent call last): File "/home/user/main.py", line 5, in <module> Poo.tell_story() TypeError: tell_story() missing 1 required positional argument: 'self'
How To Solve The “TypeError: Missing 1 Required Positional Argument: ‘self'” Error?
There are two approaches you can utilize to find the way out of such trouble. Let’s have a look!
Method #1: Call the class first.
Instantiating the class is the first and easiest fix for this. If you’re not familiar with the phrase, this indicates making an object of that class.
Although our Poo class doesn’t have a constructor, Python will provide a default constructor that does nothing.
An effective example is shown below:
class Foo: def tell_story(self): print("Good night") poo = Poo() poo.tell_story() # -> Good night
Method #2: Add a static method to the class that calls the function.
There is also a technique to create a static method, sometimes known as a “class method,” as it is occasionally termed.
Class methods can be handy for a variety of tasks, including factory methods, singletons, and general tasks that logically belong in the class but aren’t always associated with an instance of that class.
The syntax for creating a static method in Python, and more especially Python 3, is as follows:
class Foo: def tell_story(self): print("Good night") @staticmethod def tell_joke(self): print("Funny Clown") poo = Poo() poo.tell_story() # -> Good night Poo.tell_joke() # -> Funny Clown
Above is mostly all that could fix perfectly your problem regarding “TypeError: missing 1 required positional argument: ‘self’” error. Hopefully, you can now get this task done more easily. See then!