Warm tip: This article is reproduced from serverfault.com, please click

graph-Python:不能将“ self”作为唯一参数

(graph - Python : can't pass "self" as the only argument)

发布于 2020-11-30 03:53:30

我正在编写代码以简化图形。在这种情况下,我需要删除一个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函数时,发生以下错误

d2noderem
left中的文件“ /path/to/file/simplifygraphs.py”,第51行,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。

而且我还没有找到解决该问题的方法。

如何克服这类问题?

非常感谢你的帮助

Questioner
Kavindu Ravishka
Viewed
11
Santhana Krishnan 2020-11-30 13:45:44

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

你应该提供期望的参数或完全重写该函数。