小编典典

Python-TypeError:缺少1个必需的位置参数:'self'

python

我是python新手,碰壁了。我遵循了一些教程,但无法克服错误:

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'

我检查了一些教程,但似乎与我的代码没有什么不同。我唯一能想到的是python 3.3需要不同的语法。

主要技巧:

# test script

from lib.pump import Pump

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

p = Pump.getPumps()

print (p)

泵类:

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

如果我正确理解,“自我”将自动传递给构造函数和方法。我在这里做错了什么?

我正在将Windows 8与python 3.3.2一起使用


阅读 1912

收藏
2020-02-14

共1个答案

小编典典

你需要在此处实例化一个类实例。

采用

p = Pump()
p.getPumps()

小例子

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


>>> testInstance = TestClass()
in init
>>> testInstance.testFunc()
in Test Func
2020-02-14