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

前端 未结 3 837
余生分开走
余生分开走 2021-01-18 11:31

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 searche

3条回答
  •  梦毁少年i
    2021-01-18 12:01

    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.

提交回复
热议问题