TypeError:缺少 1 个必需的位置参数:'self'
问题描述
我无法克服错误:
Traceback (most recent call last):
File "C:UsersDomDesktop est est.py", line 7, in <module>
p = Pump.getPumps()
TypeError: getPumps() missing 1 required positional argument: 'self'
我检查了几个教程,但似乎与我的代码没有什么不同.我唯一能想到的是 Python 3.3 需要不同的语法.
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.
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
会自动传递给构造函数和方法.我在这里做错了什么?
If I understand correctly, self
is passed to the constructor and methods automatically. What am I doing wrong here?
解决方案
这里需要实例化一个类实例.
You need to instantiate a class instance here.
使用
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
相关文章