【问题标题】:TypeError: Missing 1 required positional argument: 'self'TypeError:缺少 1 个必需的位置参数:'self'
【发布时间】:2013-07-06 05:46:07
【问题描述】:

我无法克服错误:

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 会自动传递给构造函数和方法。我在这里做错了什么?

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    Python 中的 self 关键字类似于 C++ / Java / C# 中的 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()
    

    【讨论】:

    • 不是关键字,只是约定。
    • “在 Python 2 中它是由编译器隐式完成的” 是什么意思? AFAIK Python 2 从未隐式设置 self
    • 这不是有效的 Python 代码。你想确切地证明什么?首先,Python cmets 使用#,而不是//;您不需要在类级别声明成员属性;那些管道似乎不合适。
    【解决方案2】:

    你可以调用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方法。

    【讨论】:

      【解决方案3】:

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

      Pump().getPumps()
      

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

      【讨论】:

      • 这实际上是 JBernardo's answer 的副本。请注意,他也不会保存实例,除非getPumps() 返回self,这很奇怪。
      【解决方案4】:

      你需要在这里实例化一个类实例。

      使用

      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
      

      【讨论】:

        【解决方案5】:

        你也可以通过过早地接受 PyCharm 的建议来注释方法 @staticmethod 得到这个错误。删除注释。

        【讨论】:

          【解决方案6】:

          你需要先初始化它:

          p = Pump().getPumps()
          

          【讨论】:

            猜你喜欢
            • 2022-12-11
            • 2020-02-15
            • 2021-12-15
            • 2023-02-06
            • 1970-01-01
            • 2021-09-15
            • 2022-01-05
            相关资源
            最近更新 更多