In Python, we first need to initialize an object for a class before we call any of the methods defined inside the class. We can access the class variables using the class name followed by the dot operator and variable name. But if we try to access a class method using the class name, we will encounter the error
TypeError: Method_Name() missing 1 required positional argument: 'self'
.
In this guide, we will discuss the above error in detail and learn why it arises in Python. We will also walk through some common examples where many Python learners encounter this error.
So without further ado, let's get started.
Python Error - TypeError: Method_Name() missing 1 required positional argument: 'self'
A class is a blueprint for objects, and its functionalities come into existence when we initialize its objects. If there is a method inside a class, we can only call that method using the class object and not with the class name.
This is because, in
Python class
, all methods have a fixed argument value
self
(conventional name), representing the object calling the method. When we call the method using the class name, we receive the error
TypeError: Method_Name() missing 1 required positional argument: 'self'
.
Now, let's break the error statement into two parts.
- TypeError
- Method_Name() missing 1 required positional argument: 'self'
1. TypeError
TypeError is a standard Python exception. It occurs when we perform an operation or call a function/method on an inappropriate Python data object.
2. Method_Name() missing 1 required positional argument: 'self'
This error message occurs when we try to call a method using the class name. We can only call a class method using its object. All class methods have a fixed argument
self
that needs to be passed to the method when called. And the value of
self
can only be passed to the method when the method is called with the help of the class object because the class object is the value for the
self
attribute.
Common Example Scenario
There are two most common cases when you might encounter the above error.
- Calling a method using Class Name
- Incorrect Initialization of Class Object
1. Calling a method using a Class Name
The core reason why we receive this error is when we use the class name to call a method rather than the object name.
Example
Output
Break the code
In the above example, we get the error because we called the
show_data()
method using the class name
Human
. When a method is invoked, it accepts the value for the
self
attribute, and it can only be satisfied when an object of the class calls the method. The object that calls the method becomes the value for the
self
attribute.
Solution
To solve the above method, we first need to initialize the object for the
Human
class. Later, we can use that object name to call the
show_data()
method.
Output
2. Incorrect Initialization of class Object
This error also occurs when we do not properly initialize the object for a class. When we initialize or declare a class object, we write the object name, followed by the assignment operator, the class name, and a set of parenthesis. When we forget to put the parenthesis, it will not initialize the class; instead, it will provide an alternative name to the class.
Example
Output
Break the Code
In this example, we get the same error. This is because, in line 12, when we initialized the object for the Human class, we forgot to put the parenthesis
()
after the class name "
human = Human
" .
The statement
human = Human
did not create the object
human
; instead, it simply provides an alternative name to the
Human
class. At this point, both
Human
and
human
are the same.
Solution
To solve the above example, we need to properly initialize the object for the Human class.
Output
Wrapping Up!
This was all about one of the most common Python errors " TypeError: method_name() missing 1 required positional argument: 'self'". This error occurs in a Python program when we call a class method with the help of the class name rather than its object. To solve this error, make sure that you have initialized the class object properly before calling the method on that object. In most cases, you will encounter this error when you haven't declared the object correctly.
If you still get this error in your Python program, you can share your code and query in the comment section. We will try to help you with debugging.
People are also reading:
- Python TypeError: cannot unpack non-iterable NoneType object Solution
- Function Argument in Python
- Python TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str’ Solution
- Arrays in Python
- Python RecursionError: maximum recursion depth exceeded while calling a Python object
- Dictionary in Python
- Python TypeError: ‘float’ object is not subscriptable Solution
- Tuple in Python
- Python typeerror: ‘list’ object is not callable Solution
- List Comprehension in Python
Leave a Comment on this Post