Functions are blocks of code that work and behave together under a name. Built-in functions have their functionality predefined. To call a built-in function, you need to use parentheses ()
. If you do not use parentheses, the Python interpreter cannot distinguish function calls from other operations such as indexing on a list object.
Using square brackets instead of parentheses to call a built-in function will raise the “TypeError: ‘builtin_function_or_method’ object is not subscriptable”.
In this tutorial, we will go into detail on the error definition. We will go through an example scenario of raising the error and how to solve it.
Table of contents
TypeError: ‘builtin_function_or_method’ object is not subscriptable
Two parts of the error tell you what has gone wrong. TypeError occurs whenever we try to perform an illegal operation for a specific data type. For example, trying to iterate over a non-iterable object, like an integer will raise the error: “TypeError: ‘int’ object is not iterable“.
The part “‘builtin_function_or_method’ object is not subscriptable” occurs when we try to access the elements of a built-in function, which is not possible because it is a non-subscriptable object. Accessing elements is only suitable for subscriptable objects like strings, lists, dictionaries, and tuples. Subscriptable objects implement the __getitem__()
method, non-subscriptable objects do not implement the __getitem__()
method.
Let’s look at the correct use of indexing on a string:
string = "Machine Learning" print(string[0])
Example: Using the Built-in sum Function with Square Brackets
Let’s write a program that defines an array of integers and a variable that stores the sum of the integers in the array. The sum()
function calculates the sum of Python container objects, including lists, tuples and dictionaries.
numbers = [10, 4, 2, 5, 7] total = sum[numbers] print(total)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) total = sum[numbers] TypeError: 'builtin_function_or_method' object is not subscriptable
In this code, we are trying to sum the integers in the array called numbers, but we are using square brackets []
instead of parenthesis ()
, which tells the Python interpreter to treat sum
like a subscriptable object. But indexing is illegal for built-in functions because they are not containers of objects.
Solution
To solve the problem, we replace the square brackets with parentheses after the function name:
numbers = [10, 4, 2, 5, 7] total = sum(numbers) print(total)
28
Our code successfully calculated the sum of the integers in the array and printed the sum value to the console.
Summary
Congratulations on reading to the end of this tutorial.
For further reading on not subscriptable errors, go to the articles:
- How to Solve Python TypeError: ‘method’ object is not subscriptable
- How to Solve Python TypeError: ‘Response’ object is not subscriptable
Go to the online courses Python page to learn more about Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.