Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:缺少1个必需的位置参数:';自我';_Python_Python 3.x - Fatal编程技术网

Python TypeError:缺少1个必需的位置参数:';自我';

Python TypeError:缺少1个必需的位置参数:';自我';,python,python-3.x,Python,Python 3.x,我无法通过错误: 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' 如果我理解正确,self会自动传递给构造函数和方法。我在这里做错

我无法通过错误:

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'

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

您需要在这里实例化一个类实例

使用

小例子-

>>> class TestClass:
        def __init__(self):
            print("in init")
        def testFunc(self):
            print("in Test Func")


>>> testInstance = TestClass()
in init
>>> testInstance.testFunc()
in Test Func

您需要首先对其进行初始化:

p = Pump().getPumps()

您也可以通过过早地接受PyCharm的建议来注释@staticmethod方法来获得此错误。删除注释。

< P> Python中的<代码>自< /Cord>关键字类似于代码> C++中的/<代码/>爪哇> /< 在Python2中,它是由编译器隐式完成的(是的,Python在内部进行编译)。 只是在Python3中,您需要在构造函数和成员函数中显式地提到它。例如:

 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()

比我在这里看到的任何其他解决方案都有效且更简单:

Pump().getPumps()

如果您不需要重用类实例,这是非常好的。在Python3.7.3上测试。

您可以调用类似于
pump.getPumps()
的方法。通过在方法上添加
@classmethod
装饰器。类方法接收类作为隐式第一个参数,就像实例方法接收实例一样

class Pump:

def __init__(self):
    print ("init") # never prints

@classmethod
def getPumps(cls):
            # Open database connection
            # some stuff here that never gets executed because of error
因此,只需调用
Pump.getPumps()


在java中,它被称为
static
方法。

以前尝试过这个方法,但缺少“()”。这在Python3.x中是新出现的吗?@DominicM:Nope,这一直存在。是的,回顾我遵循的教程,我的大脑一定是把括号遮住了:)救了我的命!谢谢:))这不是一个关键字,只是一个约定。你说“在Python2中它是由编译器隐式完成的”是什么意思?AFAIK Python 2从不隐式设置
self
。这是无效的Python代码。你到底想证明什么?首先,Python注释使用
#
,而不是
/
;您不需要在类级别声明成员属性;这些管道似乎不合适。这实际上是一个复制品。请注意,他也不会保存实例,除非
getPumps()
返回
self
,这会很奇怪。
Pump().getPumps()
class Pump:

def __init__(self):
    print ("init") # never prints

@classmethod
def getPumps(cls):
            # Open database connection
            # some stuff here that never gets executed because of error