Home TypeError: Missing 1 required positional argument: 'self'
Reply: 3

TypeError: Missing 1 required positional argument: 'self'

DominicM
1#
DominicM Published in 2013-07-08 19:21:42Z

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

Sukrit Kalra
2#
Sukrit Kalra Reply to 2014-05-06 16:32:03Z

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
JBernardo
3#
JBernardo Reply to 2013-07-08 19:23:10Z

You need to initialize it first:

p = Pump().getPumps()
gherson
4#
gherson Reply to 2017-12-03 15:58:11Z

You can also get this error by prematurely taking PyCharm's advice to annotate a method @staticmethod. Remove the annotation.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.409513 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO