TypeError: Missing 1 required positional argument: 'self'

DominicM picture DominicM · Jul 8, 2013 · Viewed 831.5k times · Source

I am new to python and have hit a wall. I followed several tutorials but cant 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.

main scipt:

# test script

from lib.pump import Pump

print ("THIS IS A TEST OF PYTHON") # this prints

p = Pump.getPumps()

print (p)

Pump class:

import pymysql

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

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

I am using windows 8 with python 3.3.2

Answer

Sukrit Kalra picture Sukrit Kalra · Jul 8, 2013

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