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

pythonpython-3.x

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?

Best 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