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

Python : can't pass "self" as the only argument

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

I'm writing a code to simplify a graph. In this case I've needed to remove a node of degree 2 and connect its two neighbors each other. Here is the simplified code

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

when I call that d2noderem function the following error occurs

File "/path/to/file/simplifygraphs.py", line 51, in d2noderem
left.remngh(self)

TypeError: remngh() missing 1 required positional argument: 'nd'

Then I tried with

left.remngh(self,self)

and this is the result

File "/path/to/file/simplifygraphs.py", line 51, in d2noderem
left.remngh(self,self)

TypeError: remngh() takes 2 positional arguments but 3 were given

I can't understand how did the no of args increased from 0 to 3 by adding 1 more argument.

And I couldn't find a solution for this problem yet.

How to overcome this type of problem?

I appreciate your help very much

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

The method 'remng' expects an argument as defined by the parameter 'nd' in def remngh(self,nd): Since you're calling it without supplying the expected argument, it's throwing an error.

You should either provide the expected argument or rewrite the function entirely.