Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

TypeError: Missing 1 required positional argument: 'self'

I can't move beyond the mistake:

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 sole thing I can consider is that python 3.3 requires a special 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?
by

2 Answers

akshay1995
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
sandhya6gczb
You need to initialize it first:

p = Pump().getPumps()

Login / Signup to Answer the Question.