簡體   English   中英

類型錯誤:缺少 1 個必需的位置參數:'self'

[英]TypeError: Missing 1 required positional argument: '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'

我檢查了幾個教程,但似乎與我的代碼沒有什么不同。 我唯一能想到的是 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會自動傳遞給構造函數和方法。 我在這里做錯了什么?

您需要在此處實例化一個 class 實例。

利用

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

你需要先初始化它:

p = Pump().getPumps()

工作並且比我在這里看到的所有其他解決方案都簡單:

Pump().getPumps()

如果您不需要重用 class 實例,那就太好了。 在 Python 3.7.3 上測試。

Python 中的self關鍵字類似於 C++ / Java / ZD7EFA19FBE7D39772FD.DB60242 中的this關鍵字

在 Python 2 中,它由編譯器隱式完成(是的 Python 在內部進行編譯)。 只是在 Python 3 中需要在構造函數和成員函數中明確提及。 例子:

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

您還可以通過過早采用 PyCharm 的建議來注釋方法 @staticmethod 來獲得此錯誤。 刪除注釋。

您可以調用類似pump.getPumps()的方法。 通過在方法上添加@classmethod裝飾器。 class 方法接收 class 作為隱式第一個參數,就像實例方法接收實例一樣。

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方法。

我在下面遇到了同樣的錯誤:

TypeError: test() 缺少 1 個必需的位置參數:'self'

一個實例方法self時,我直接通過 class 名稱調用它,如下所示:

class Person:
    def test(self): # <- With "self" 
        print("Test")

Person.test() # Here

而且,當static 方法self時,我通過 object 或直接通過 class 名稱調用它,如下所示:

class Person:
    @staticmethod
    def test(self): # <- With "self" 
        print("Test")

obj = Person()
obj.test() # Here

# Or

Person.test() # Here

所以,我用 object 調用了實例方法,如下所示:

class Person:
    def test(self): # <- With "self" 
        print("Test")

obj = Person()
obj.test() # Here

而且,我從 static 方法中刪除了self ,如下所示:

class Person:
    @staticmethod
    def test(): # <- "self" removed 
        print("Test")

obj = Person()
obj.test() # Here

# Or

Person.test() # Here

然后,錯誤解決了:

Test

詳細地說,我在 Python 中對什么是“實例方法”的回答中解釋了實例方法 並在我對@classmethod vs @staticmethod in Python 的回答中解釋@staticmethod@classmethod

如果跳過 object 聲明(拼寫錯誤)的括號,則會發生此錯誤。

# WRONG! will result in TypeError: getPumps() missing 1 required positional argument: 'self'
p = Pump
p.getPumps()

不要忘記泵 object 的括號

# CORRECT!
p = Pump()
p.getPumps()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM