Fix Python – TypeError: Missing 1 required positional argument: ‘self’

Question

Asked By – DominicM

I can’t get past the error:

Traceback (most recent call last):
  File "C:\Users\Dom\Desktop\test\test.py", line 7, in <module>
    p = Pump.getPumps()
TypeError: getPumps() missing 1 required positional argument: 'self'

I examined several tutorials but there doesn’t seem to be anything different from my code. The only thing I can think of is that Python 3.3 requires different syntax.

class Pump:

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

    def getPumps(self):
        # Open database connection
        # some stuff here that never gets executed because of error
        pass  # dummy code

p = Pump.getPumps()

print(p)

If I understand correctly, self is passed to the constructor and methods automatically. What am I doing wrong here?

Now we will see solution for issue: TypeError: Missing 1 required positional argument: ‘self’


Answer

You need to instantiate a class instance here.

Use

p = Pump()
p.getPumps()

Small example –

>>> class TestClass:
        def __init__(self):
            print("in init")
        def testFunc(self):
            print("in Test Func")


>>> testInstance = TestClass()
in init
>>> testInstance.testFunc()
in Test Func

This question is answered By – Sukrit Kalra

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0