【问题标题】:"TypeError: missing 1 required positional argument" Instance error [duplicate]“TypeError:缺少 1 个必需的位置参数”实例错误 [重复]
【发布时间】:2019-03-05 12:13:13
【问题描述】:

我有两个类,在第一个类中,我创建了第二个类的一个实例,然后执行第二个类的一个方法,一个启动进程的方法。

t1.py

test = "t1"

def executeBase():
    base = baseNode.BaseNode()
    baseNode.BaseNode.executeBase(test, base) #error

和baseNode.py

class BaseNode():

    def __init__(self):
        self.eui48 = "01:00:00:00:00:00"
        self.port = 7919

    def executeBase(self, test, base):
        #I execute here a process

我在一行中遇到错误 (#error)。

  File "/testbench/testbenchPython/test/t1.py", line 20, in executeBase
baseNode.BaseNode.executeBase(test, base)
TypeError: executeBase() missing 1 required positional argument: 'base'

不能这样做吗?如果是,有什么问题,我该如何纠正? 我尝试以不同的方式传递参数,但没有找到解决方案。

非常感谢!

【问题讨论】:

    标签: python object arguments instance


    【解决方案1】:

    你应该在你的实例上调用executeBase。试试这个:

    def executeBase():
        base = baseNode.BaseNode()
        base.executeBase(test, base)
    

    【讨论】:

    • 这行得通,但对我来说使用实例来调用方法并在方法本身中传递实例似乎有点奇怪。无论如何,谢谢!
    • 你可以在你的代码上运行baseNode.BaseNode () .executeBase(test, base) 或者只使用基本实例属性——你是对的,你不需要传递给实例到自己。
    【解决方案2】:

    在这种情况下

    def executeBase(): base = baseNode.BaseNode() baseNode.BaseNode.executeBase(test, base) #error

    我猜 executeBase() 函数正在寻找自我!而不是baseNode.BaseNode.executeBase(test, base) #error 这一行这可能会工作base.executeBase(test,base)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-06
      • 2019-06-25
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多