问题:TypeError:缺少1个必需的位置参数:’self’

我是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'

我检查了一些教程,但似乎与我的代码没有什么不同。我唯一能想到的是python 3.3需要不同的语法。

主要技巧:

# test script

from lib.pump import Pump

print ("THIS IS A TEST OF PYTHON") # this prints

p = Pump.getPumps()

print (p)

泵类:

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

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

我正在将Windows 8与python 3.3.2一起使用

I am new to python and have hit a wall. I followed several tutorials but cant get past the error:

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.

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?

I am using windows 8 with python 3.3.2


回答 0

您需要在此处实例化一个类实例。

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

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

回答 1

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

p = Pump().getPumps()

You need to initialize it first:

p = Pump().getPumps()

回答 2

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

Pump().getPumps()

如果您不需要重用类实例,那么这很好。在Python 3.7.3上测试。

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.


回答 3

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

You can also get this error by prematurely taking PyCharm’s advice to annotate a method @staticmethod. Remove the annotation.


回答 4

python中的‘self’关键字类似于c ++ / java / c#中的‘this’关键字。

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

The self keyword in Python is analogous to this keyword in C++ / Java / C#.

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:

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。