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”. If you try to use square brackets when you call a method that belongs to a class you created, you will encounter a similar error: “TypeError: ‘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.


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. We raise the error “TypeError: ‘builtin_function_or_method’ object is not subscriptable” whenever we use square brackets [] to call a Python built-in function instead of using parentheses (). Built-in functions do not contain other objects, and therefore they are not subscriptable objects. To solve this we need to find the call in the code and ensure we replace the square brackets with parentheses.

Go to the online courses Python page to learn more about Python for data science and machine learning.

Have fun and happy researching!