MacKay Killian
MacKay Killian

Reputation: 3

Class method not executing recursively

I have this class method I have written for traversing a graph object, but when it reaches the line I commented #HERE!! it doesn't seem to execute it. When I run the debugger the if statement beforehand returns true, and it reaches the recursive call however it treats it as if nothing is there. Am I using the wrong syntax for recursively calling within a class method?

    def dfs(self, start):
        
        start_vert = self.vert_list[start]
        start_vert.set_color("gray")
        yield start_vert.label
        for vertex in self.vert_list.values():
            vertex.set_color("white")

        for nbr in start_vert.get_neighbors():
            next_vert = self.vert_list[nbr]
            if next_vert.color == "white":
                self.dfs(next_vert.label) #HERE!!
        start_vert.set_color("black")

Upvotes: 0

Views: 38

Answers (1)

cidermole
cidermole

Reputation: 6078

What you are writing is a generator, not a simple class method - you can see this by the yield keyword inside the method.

Your generator is not doing anything with the values that the recursive call generates.

What you probably wanted to do, was to iterate over those values, maybe this:

if next_vert.color == "white":
    for label in self.dfs(next_vert.label):
        yield label

It is not possible to know exactly, without you stating the algorithm that you're working on.

Upvotes: 1

Related Questions