1

I am novice to python and I am trying to understand a basic error here . I am getting a TypeError: 'list' object is not callable error in the below code . Can somebody explain me what is wrong in my code ?

graph = {'a': ['b', 'c'], 'b': ['a', 'c'], 'c': ['b', 'd'], 'd': ['a'], 'e': ['a']}


def reachable(graph, node):
    res = [node]
    reachable = graph[node]
    for currentnode in reachable:
        if currentnode not in res :
            reachableNodes = reachable(graph,currentnode) << TypeError: 
            for newNode in reachableNodes:
                if newNode not in res :
                    res.append(newNode)
    return res 

Error : TypeError: 'list' object is not callable error

3
  • I think a bit more context is necessary - what is this function supposed to do? How are you calling it? Note that the indentation of the return statement is wrong. Commented Apr 8, 2012 at 20:05
  • what is your graph parameter? it seems the graph[node] returns a list object and you call the list instance illegally like functions, and see the danniel's answer Commented Apr 8, 2012 at 20:07
  • I have added what graph will look like Commented Apr 8, 2012 at 20:09

3 Answers 3

8

You've hidden the function name by doing reachable = graph[node]. Use a different name.

Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean hidden the function name . It is a recursive call ? Can you please explain a little bit more
I don't know what being recursive has got to do with it. You've assigned the list element to the name reachable. Only one thing at a time can have that name. So the next time you refer to reachable, Python only knows about the list, not the function. Call the list something else.
@rush00121: Lets put it this way: What do you want reachable to refer to in reachable(graph,currentnode) (where the error is)?
2

reachable is your module name which you are calling recursively.

At line 3, when you say reachable = graph[node], it overwrites the variable reachable which was bounded to a function to be now linked to a list (or what ever).

When at line 6, you try to call the function recursively, it ends up trying to call the list which is what reachable is and it fails.

To solve this change the name of the variable, you are intending to hold the list to something different from reachable

canreach = graph[node]
for currentnode in canreach:

Also double check your reachable function. There is a possibility of infinite recursion. Every time you are recursively calling reachable, you are creating a new instance of res. So if currentnode not in res is never false. Try passing res as a parameter, or use it as a global.

Comments

0

I restarted the Karnel in JpyterNotebook, and have solved the problem without any change.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.