TypeError: ‘dict’ object is not callable
TypeError: ‘dict’ object is not callable
In this article we will learn about the TypeError: ‘dict’ object is not callable.
This error is generated when we try to call a dictionary using invalid methods. As shown in the example below.
Example:
Output:
In the above example, we can see that in line 9 of the code i.e print(MyDict()), we called our dictionary using parenthesis. And thus causing the error, “TypeError: ‘dict’ object is not callable”.
Now the question arises why we can’t call the dictionary using parenthesis ( )?
To understand this let us take the help of a ‘dir( )’ a built-in method. Which returns the list of all the functions/methods associated with the object.
Example:
The output shows the list of all the functions associated with a dictionary object. To make an object callable the presence of a magic function(__call__) is a must.
We can see in the above output, the magic function (__call__) is not defined or is absent. The absence of this magic function is what made our dictionary uncallable. Thus the dictionary object can not be invoked using parenthesis( ).
Solution:
Do print(MyDict) instead of print(MyDict( )) in line 9 of the code.
Python TypeError: 'dict' object is not callable solution
Dictionary is a standard Python data structure that stores the elements in the form of key:value pairs. To access an individual item from the dictionary we put the key name inside a square bracket [] . But if we use the parenthesis () we will receive the «TypeError: ‘dict’ object is not callable» .
In this guide, we will discuss the «dict object is not callable» error in detail and learn why Python raises it. We will also walk through a common case scenario where you may encounter this error.
By the end of this error solution tutorial, you will have a complete idea of why this error raises in a Python program and how to solve it.
Python Error- TypeError: ‘dict’ object is not callable
Python Dictionary is a mutable data structure, and it has a data type of dict . It follows the syntax of the square bracket to access an individual element.
Example
But if we use parenthesis () instead of the square bracket [] we will receive an error.
Error Example
Error Statement
This error statement has two parts «TypeError» and » ‘dict’ object is not callable» TypeError is the Exception Type telling us that we are performing some invalid operation on a Python data object.
In the above example, we are receiving this exception because we can not use parenthesis to access dictionary elements. ‘dict’ object is not callable means we are trying to call a dictionary object as a function or method.
In Python functions and methods are callable objects, we put the parenthesis () after their name when we want to call them. But the dictionary is not a function or method, and when we put the parenthesis after the dictionary name Python throws an error.
Common Example Scenario
Now let’s discuss an example where you may encounter this error in your Python code. Let’s say we have a dictionary human that contains some information about the human species and we need to print all that information on the console panel.
Example
Output
Break the Error In the above example we are getting the TypeError: ‘dict’ object is not callable because we have used the () brackets to access the data value from the dictionary human .
Solution
To solve the above example, we need to replace the () brackets with [] brackets, while we are accessing the dictionary value using key.
Output
Now our code runs successfully with no error.
Conclusion
The Error «TypeError: ‘dict’ object is not callable» error raises in a Python program when we use the () brackets to get a dictionary element. To debug this error we need to make sure that we are using the square brackets [] to access the individual element.
If you are receiving this error in your Python program and could not solve it. You can share your code in the comment section, we will try to help you in debugging.
TypeError: 'dict' object is not callable
I’m trying to loop over elements of an input string, and get them from a dictionary. What am I doing wrong?
9 Answers 9
The syntax for accessing a dict given a key is number_map[int(x)] . number_map(int(x)) would actually be a function call but since number_map is not a callable, an exception is raised.
Access the dictionary with square brackets.
You need to use [] to access elements of a dictionary. Not ()
You get an element from a dict using these [] brackets, not these () .
Use square brackets to explore dictionaries.
You need to use:
Note the square brackets!
A more functional approach would be by using dict.get
One can observe that the conversion is a little clumsy, better would be to use the abstraction of function composition:
Obviously in Python 3 you would avoid the explicit conversion to a list . A more general approach for function composition in Python can be found here.
(Remark: I came here from the Design of Computer Programs Udacity class, to write:)
How to fix "‘dict’ object is not callable" in Python
✋ Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a user experience.
The “TypeError: ‘dict’ object is not callable” error occurs when you try to call a dictionary (dict object) as if it was a function! Based on some threads on Stack Overflow, the most common cause of this error is using () rather than [] when accessing a dictionary item.
Here’s what the error looks like:
Exit fullscreen mode
Calling a dictionary object as if it’s a callable isn’t what you’d do on purpose, though. It usually happens due to a wrong syntax (as mentioned above) or overriding a builtin (or user-defined) function name with a dictionary object.
Let’s explore the common causes and their solutions.
How to fix TypeError: ‘dict’ object is not callable?
This TypeError happens under various scenarios:
- Accessing a dictionary item by () rather than []
- Declaring a dictionary with a name that’s also the name of a function
- Calling a method that’s also the name of a property
- Calling a method decorated with @property
Accessing a dictionary item by () rather than [] : The most common cause of this TypeError is accessing a dictionary item by () instead of [] .
Based on Python semantics, any identifier followed by a () is a function call. In this case, since () follows a dictionary object, it’s like you’re trying to call the dictionary like it’s callable.
As a result, you’ll get the «TypeError: ‘dict’ object is not callable» error.
Exit fullscreen mode
This is how you’re supposed to access a dictionary value:
Exit fullscreen mode
Declaring a dictionary with a name that’s also the name of a function: A Python function is an object like any other built-in object, such as str , int , float , dict , list , etc.
All built-in functions are defined in the builtins module and assigned a global name for easier access. For instance, dict() builtin function refers to the __builtins__.dict() function.
That said, overriding a function’s global name (accidentally or on purpose) with any value (e.g., a dictionary) is technically possible.
In the following example, we’ve declared a variable named range containing some config data. In its following line, we use the range() function in a for loop:
Exit fullscreen mode
If you run the above code, Python will complain with this type error because we’ve already assigned the range global variable to our first dictionary.
We have two ways to fix the issue:
- Rename the variable range
- Explicitly access the range() function from the builtins module ( __bultins__.range )
The second approach isn’t recommended unless you’re developing a module. For instance, if you want to implement an open() function that wraps the built-in open() :
Exit fullscreen mode
In almost every other case, you should always avoid naming your variables as existing functions and methods. But if you’ve done so, renaming the variable would solve the issue.
So the above example could be fixed like this:
Exit fullscreen mode
This issue is common with function names you’re more likely to use as variable names. Functions such as vars, locals, list, dict, all, or even user-defined functions.
⚠️ Long story short, you should never use a function name (built-in or user-defined) for your variables!
Overriding functions (and calling them later on) is one of the most common causes of the «TypeError: ‘dict’ object is not callable» error. It’s similar to calling integer numbers as if they’re callables.
Now, let’s get to the less common mistakes that lead to this error.
Calling a method that’s also the name of a property: When you define a property in a class constructor, it’ll shadow any other attribute of the same name.
Exit fullscreen mode
In the above example, we have a property named isbn — a dictionary to keep ISBN 10 and ISBN 13 of the book. Further down, we defined a method, also named isbn .
However the property isbn shadows the method isbn() . As a result, any reference to isbn returns the property — a dict object — not the method. And if you try to call this dict object, you should expect the «TypeError: ‘dict’ object is not callable» error.
The name get_isbn sounds like a safer and more readable alternative:
Exit fullscreen mode
Calling a method decorated with @property decorator: The @property decorator turns a method into a “getter” for a read-only attribute of the same name. You need to access a getter method without parenthesis, otherwise you’ll get a TypeError.
Exit fullscreen mode
To fix it, you need to access the getter method without the parentheses:
Exit fullscreen mode
Alright, I think it does it! I hope this quick guide helped you fix your problem.