How To Solve “Missing 1 required positional argument: ‘self’” In Python

TypeError: missing 1 required positional argument: ‘self’ in Python

Are you having difficulties with the TypeError: Missing 1 required positional argument: ‘self’ in Python? If yes, let’s follow our article. We will give you some solutions to fix it.

What is the cause of the error “Missing 1 required positional argument: ‘self’”?

This is a common error that many beginners encounter:

class TestSelf:
  	def __init__(self):
      	print("Check label")
  	def getTest(self):
      	pass  # dummy code

t = TestSelf.getTest()
print(t)

They might want that the snippet will print:

Check label
None

But the Python runtime gave the error:

Traceback (most recent call last):
  line 6, in <module>
    t = TestSelf.getTest()
TypeError: TestSelf.getTest() missing 1 required positional argument: 'self'

Why does the first program result in an error? In fact, if you fix it, “self” is automatically provided to the constructor and methods. So what is wrong here?

To understand why does this error occur and how to fix it, you need to understand how classes work in Python 3.

Class Variable

The class construction includes a definition of class variables. Since the class owns class variables, they are shared by all class instances. Therefore, unless you initialize a variable using the class variable, it will typically have the same value for every instance.

Class variables are typically defined independently of any methods and by convention, it positioned immediately below the class header and before the constructor function and other methods.

For example, we have a simple class variable:

class TestSelf:
 	test1_type = "Check label"

The value "Check label" is given to the ‘test1_type’ variable in this instance. Typically, a class contains numerous data members, so when we create an instance, print the variable by using dot notation:

class TestSelf:
  	test1_type = "Check label" 

test_type = TestSelf
print(test_type.test1_type)

Output:

Check label

Instance Variable

Instance variables are possessed by instances of the class. This implies that the instance variables are distinct for instance of a class or each object.

Don’t like as class variables, instance variables are specified within methods.

For example:

class TestSelf:
    def __init__(self, type, length):
        self.type = "Check label"
        self.length = "10"

In this example, “type” and “length” are instance variables.

These variables must be defined before they may be supplied as parameters to the constructor method or another method when creating a TestSelf object. In Python 2, the compiler performs it implicitly, but in Python 3, you must explicitly mention it in the constructor and member functions.

How to solve the error “TypeError: missing 1 required positional argument: ‘self’”?

Add the () after class name

Because there isn’t an instance variable in the article’s first example, you just need to add the () after class name ‘TestSelf’ as the following:

class TestSelf:
    def __init__(self):
        print("Check label")
    def getTest(self):
        pass  # Dummy code

t = TestSelf().getTest()
print(t)

Output:

Check label
None

Make the function a static method of the class

Make the function a static method of the class if method 1 didn’t work :

class TestSelf:
    def __init__():
        print("Check label")
    def getTest():
        pass  # Dummy code

t = TestSelf.getTest()
print(t)

Output:

None

Summary

We hope you enjoy our article about the TypeError: missing 1 required positional argument: ‘self’ in Python. If you hane any questions or problems, let’s contact us by leaving comment below. Thanks for reading! 

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *