Python 类型错误:缺少 1 个必需的位置参数:'self'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17534345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: Missing 1 required positional argument: 'self'
提问by DominicM
I am new to python and have hit a wall. I followed several tutorials but cant get past the error:
我是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'
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.
我检查了几个教程,但似乎与我的代码没有任何不同。我唯一能想到的是 python 3.3 需要不同的语法。
main scipt:
主要内容:
# test script
from lib.pump import Pump
print ("THIS IS A TEST OF PYTHON") # this prints
p = Pump.getPumps()
print (p)
Pump class:
泵类:
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
If I understand correctly "self" is passed to the constructor and methods automatically. What am I doing wrong here?
如果我理解正确,“self”会自动传递给构造函数和方法。我在这里做错了什么?
I am using windows 8 with python 3.3.2
我正在使用带有 python 3.3.2 的 Windows 8
采纳答案by Sukrit Kalra
You need to instantiate a class instance here.
您需要在此处实例化一个类实例。
Use
用
p = Pump()
p.getPumps()
Small example -
小例子——
>>> class TestClass:
def __init__(self):
print("in init")
def testFunc(self):
print("in Test Func")
>>> testInstance = TestClass()
in init
>>> testInstance.testFunc()
in Test Func
回答by JBernardo
You need to initialize it first:
你需要先初始化它:
p = Pump().getPumps()
回答by gherson
You can also get this error by prematurely taking PyCharm's advice to annotate a method @staticmethod. Remove the annotation.
您也可以通过过早地采用 PyCharm 的建议来注释方法 @staticmethod 来获得此错误。删除注释。
回答by Tahir77667
The 'self'keyword in python is analogous to 'this'keyword in c++ / java / c#.
python中的'self'关键字类似于c++/java/c#中的'this'关键字。
In python 2 it is done implicitly by the compiler (yes python does compilation internally)
.
It's just that in python 3 you need to mention it explicitly
in the constructor and member functions. example:
在 python 2 中,它是由编译器隐式完成的(yes python does compilation internally)
。只是在python 3中你需要explicitly
在构造函数和成员函数中提及它。例子:
class Pump():
//member variable
account_holder
balance_amount
// constructor
def __init__(self,ah,bal):
| self.account_holder = ah
| self.balance_amount = bal
def getPumps(self):
| print("The details of your account are:"+self.account_number + self.balance_amount)
//object = class(*passing values to constructor*)
p = Pump("Tahir",12000)
p.getPumps()
回答by Jay D.
Works and is simpler than every other solution I see here :
有效并且比我在这里看到的所有其他解决方案更简单:
Pump().getPumps()
This is great if you don't need to reuse a class instance. Tested on Python 3.7.3.
如果您不需要重用类实例,这很好。在 Python 3.7.3 上测试。