Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'dict' object is not callable while using dict( )

Tags:

python

I was using Python 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux2 , when i ran the folllowing code in it the corresponding error is shown .I searched a lot about this but am unable to find why is it so

>>> bob=dict(name='bob smith',age=42,pay='10000',job='dev')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dict' object is not callable
like image 799
Masquerade Avatar asked Jan 12 '17 05:01

Masquerade


3 Answers

In a fresh interpreter:

>>> bob=dict(name='bob smith',age=42,pay='10000',job='dev')
>>> bob
{'age': 42, 'pay': '10000', 'job': 'dev', 'name': 'bob smith'}

However, you are getting a TypeError:

TypeError: 'dict' object is not callable

This error you get tells you that your dict is not callable.

Since my dict is callable when I open a fresh interpreter, it means that your dict is different.

Most likely, you defined a dict variable, which overrode the built-in dict. Look for the

dict = {...}

line, and rename your variable.

As pointed out by @Robᵩ, don't use built-in names for your variables. Especially avoid the tempting str, list, and so on.

like image 105
Right leg Avatar answered Nov 16 '22 00:11

Right leg


On a previous line in that interactive session, you have rebound the dict name to some variable. Perhaps you have a line like dict={1:2} or dict=dict(one=1, two=2).

Here is one such session:

>>> dict=dict(one=1)
>>> bob=dict(name='bob smith',age=42,pay='10000',job='dev')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dict' object is not callable
>>> 

As a general rule, one should not use built-in type names as variable names, to prevent this error.

like image 44
Robᵩ Avatar answered Nov 16 '22 00:11

Robᵩ


edit: Ignore this, I am told this is bad practice.

As mgilson stated, the issue is likely that you have a variable called dict. The solution to this would be to run

del dict

which deletes the variable by that name.

like image 35
Peter Gottesman Avatar answered Nov 16 '22 01:11

Peter Gottesman