python - 类型错误 : Missing 1 required positional argu

我无法克服错误:

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 需要不同的语法。

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)

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

最佳答案

你需要在这里实例化一个类实例。

使用

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

关于python - 类型错误 : Missing 1 required positional argument: 'self' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17534345/

相关文章:

python - 为什么 apt-get 功能在 Mac OS X v10.9 (Mavericks

python - 什么是字典 View 对象?

linux - 如何在 linux bash/shell 中对图像进行 base64 编码

python - 如何将一串字节转换为int?

linux - 在 printf 中使用颜色

linux - 我可以在已编译的二进制文件中更改 'rpath' 吗?

python - 如何检查两个日期之间的差异(以秒为单位)?

linux - 使用 bash 历史记录获取先前的命令,复制它,然后 'run' 它但命令注释

linux - GNU screen 没有响应,似乎被阻塞了

python - 我如何捕捉一个像异常一样的 numpy 警告(不仅仅是为了测试)?