I am novice to Python and following a tutorial. There is an example of list
in the tutorial :
example = list('easyhoss')
Now, In tutorial, example= ['e','a',...,'s']
. But in my case I am getting following error:
>>> example = list('easyhoss')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
Please tell me where I am wrong. I searched SO this but it is different.
For me it was a flask server returning some
videos
array (which I expected to be in json format..)adding
json.dumps(videos)
fixed this issueYou may have used built-in name 'list' for a variable in your code. If you are using Jupyter notebook, sometimes even if you change the name of that variable from 'list' to something different and rerun that cell, you may still get the error. In this case you need to restart the Kernal. In order to make sure that the name has change, click on the word 'list' when you are creating a list object and press Shit+Tab, and check if Docstring shows it as an empty list.
Seems like you've shadowed the built-in name
list
by an instance name somewhere in your code.I believe this is fairly obvious. Python stores object names (functions and classes are objects, too) in dictionaries (namespaces are implemented as dictionaries), hence you can rewrite pretty much any name in any scope. It won't show up as an error of some sort. You'd better use an IDE (e.g. a free version of PyCharm or Atom with Python plugins) that highlights name shadowing.
EDIT. Thanks to your additional questions I think the whole thing about builtins and scoping is worth some additional coverage. As you might know Python emphasizes that "special cases aren't special enough to break the rules". And there are a couple of rules behind the problem you've faced.
Namespaces. Python supports nested namespaces. Theoretically you can endlessly nest namespaces. As I've already mentioned, namespaces are basically dictionaries of names and references to corresponding objects. Any module you create gets its own "global" namespace. In fact it's just a local namespace with respect to that particular module.
Scoping. When you reference a name, the Python runtime looks it up in the local namespace (with respect to the reference) and, if such name does not exist, it repeats the attempt in a higher-level namespace. This process continues until there are no higher namespaces left. In that case you get a
NameError
. Builtin functions and classes reside in a special high-order namespace__builtins__
. If you declare a variable namedlist
in your module's global namespace, the interpreter will never search for that name in a higher-level namespace (that is__builtins__
). Similarly, suppose you create a variablevar
inside a function in your module, and another variablevar
in the module. Then, if you referencevar
inside the function, you will never get the globalvar
, because there is avar
in the local namespace - the interpreter has no need to search it elsewhere.Here is a simple illustration.
So, as you see there is nothing special about Python builtins. And your case is a mere example of universal rules.
P.S.
When you start an interactive Python session you create a temporary module.
If you are in a interactive session and don't want to restart you can remove the shadowing with