To call a built-in function, you need to use parentheses. Parentheses distinguish function calls from other operations that can be performed on some objects, like indexing.
If you try to use square brackets to call a built-in function, you’ll encounter the “TypeError: ‘builtin_function_or_method’ object is not subscriptable” error.
In this guide, we talk about what this error means and why you may encounter it. We’ll walk through an example so that you can figure out how to solve the error.
TypeError: ‘builtin_function_or_method’ object is not subscriptable
Only iterable objects are subscriptable. Examples of iterable objects include lists, strings, and dictionaries. Individual values in these objects can be accessed using indexing. This is because items within an iterable object have index values.
Consider the following code:
languages = ["English", "French"] print(languages[0])
Our code returns “English”. Our code retrieves the first item in our list, which is the item at index position 0. Our list is subscriptable so we can access it using square brackets.
Built-in functions are not subscriptable. This is because they do not return a list of objects that can be accessed using indexing.
The “TypeError: ‘builtin_function_or_method’ object is not subscriptable” error occurs when you try to access a built-in function using square brackets. This is because when the Python interpreter sees square brackets it tries to access items from a value as if that value is iterable.
An Example Scenario
We’re going to build a program that appends all of the records from a list of homeware goods to another list. An item should only be added to the next list if that item is in stock.
Start by defining a list of homeware goods and a list to store those that are in stock:
homewares = [ { "name": "Gray Lampshade", "in_stock": True }, { "name": "Black Wardrobe", "in_stock": True }, { "name": "Black Bedside Cabinet", "in_stock": False } ] in_stock = []
The “in_stock” list is presently empty. This is because we have not yet calculated which items in stock should be added to the list.
Next, we use a for loop to find items in the “homewares” list that are in stock. We’ll add those items to the “in_stock” list:
for h in homewares: if h["in_stock"] == True: in_stock.append[h] print(in_stock)
We use the append() method to add a record to the “in_stock” list if that item is in stock. Otherwise, our program does not add a record to the “in_stock” list. Our program then prints out all of the objects in the “in_stock” list.
Let’s run our code and see what happens:
Traceback (most recent call last): File "main.py", line 10, in <module> in_stock.append[h] TypeError: 'builtin_function_or_method' object is not subscriptable
Our code returns an error.
The Solution
Take a look at the line of code that Python points to in the error:
in_stock.append[h]
We’ve tried to use indexing syntax to add an item to our “in_stock” list variable. This is incorrect because functions are not iterable objects. To call a function, we need to use parenthesis.
We fix this problem by replacing the square brackets with parentheses:
in_stock.append(h)
Let’s run our code:
[{'name': 'Gray Lampshade', 'in_stock': True}, {'name': 'Black Wardrobe', 'in_stock': True}]
Our code successfully calculates the items in stock. Those items are added to the “in_stock” list which is printed to the console.
Conclusion
The “TypeError: ‘builtin_function_or_method’ object is not subscriptable” error is raised when you try to use square brackets to call a function.
This error is raised because Python interprets square brackets as a way of accessing items from an iterable object. Functions must be called using parenthesis. To fix this problem, make sure you call a function using parenthesis.
Now you’re ready to solve this common Python error like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.