我无法克服错误:
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/