【问题标题】:Python : can't pass "self" as the only argumentPython:不能将“self”作为唯一参数传递
【发布时间】:2021-03-12 00:58:33
【问题描述】:

我正在编写代码来简化图表。在这种情况下,我需要删除一个 2 阶节点并将其两个邻居相互连接。这是简化的代码

class node():
    
    def __init__(self,ind):
        #some code

        self.neighbors=queue()  #queue is another class defined by me
        self.distances=queue()

        #some code
        
        
    def addngh(self,nd,distance):
        #some code
            
    def remngh(self,nd):           #remove node "nd" from neighbors queue
        #some code
        
    def d2noderem(self):           #removing self node from its left,right neighbors' "neighbors" queue by passing self to left and right's "remngh" function
        
        left,right = self.neighbors[0:2]

        #some code
        
        left.remngh(self)  #======= Error occurs at here ==========
        right.remngh(self)
        
        #some code

当我调用 d2noderem 函数时,会发生以下错误

文件“/path/to/file/simplifygraphs.py”,第 51 行,在 d2noderem
left.remngh(self)

TypeError: remngh() 缺少 1 个必需的位置参数:'nd'

然后我尝试了

left.remngh(self,self)

这就是结果

文件“/path/to/file/simplifygraphs.py”,第 51 行,在 d2noderem
left.remngh(self,self)

TypeError: remngh() 接受 2 个位置参数,但给出了 3 个

我不明白如何通过添加 1 个参数将 args 的数量从 0 增加到 3。

我还没有找到解决这个问题的方法。

如何克服这类问题?

非常感谢您的帮助

【问题讨论】:

    标签: python-3.x graph


    【解决方案1】:

    方法 'remng' 需要一个由 def remngh(self,nd): 中的参数 'nd' 定义的参数,因为您在没有提供预期参数的情况下调用它,所以它会引发错误。

    您应该提供预期的参数或完全重写函数。

    【讨论】:

      猜你喜欢
      • 2014-08-21
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      相关资源
      最近更新 更多