close

[Solved] TypeError: Missing 1 required positional argument: ‘self’

Hello Guys, How are you all? Hope You all Are Fine. Today I face TypeError: Missing 1 required positional argument: ‘self’ in Python. So Here I am Explain to you all the possible solutions here.

Without Wasting your time, Lets start This Article to Solve This Error.

How This TypeError: Missing 1 required positional argument: ‘self’ Occurs ?

I have Simple Code here.

class MyClass:

    def __init__(self):
        print("init") # never prints

    def getData(self):
        print(self)
        pass

p = MyClass.getData()

print(p)

Here is I got TypeError.

TypeError: getData() missing 1 required positional argument: 'self'

How To Solve TypeError: Missing 1 required positional argument: ‘self’ Error ?

Question: How To Solve TypeError: Missing 1 required positional argument: ‘self’ Error ?
Answer: To Solve TypeError: Missing 1 required positional argument: ‘self’ You need to instantiate a class instance here. You need to initialize it first. You should use as below.

Solution 1

You need to instantiate a class instance here. You should use as below.

m = MyClass()
m.getData()

Example

>>> class MyClass:
        def __init__(self):
            print("in init")
        def getData(self):
            print("in get data Func")


>>> myInstance = MyClass()
in init
>>> myInstance.getData()
in get data Func

Solution 2

You need to initialize it first:

d = MyClass().getData()

Solution 3

Works and is simpler than every other solution I see here :

MyClass().getData()

This is great if you don’t need to reuse a class instance. Tested on Python 3.7.3.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment