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

发布于 2021-02-02 23:20:17

我是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一起使用

关注者
0
被浏览
72
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

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

    采用

    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
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看