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
Let’s look at the correct use of indexing on a string:
string = "Machine Learning" print(string)
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.
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)
Our code successfully calculated the sum of the integers in the array and printed the sum value to the console.
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!