Skip to main content

How to Fix builtin_function_or_method' object is not subscriptable

To fix the "'builtin_function_or_method' object is not subscriptable" error, ensure you are calling the function or method with parentheses () instead of square brackets [].
Jul 1, 2024  · 6 min read

In Python, a common error message is the following:

'builtin_function_or_method' object is not subscriptable

Python raises this error when a function or method is followed by the square brackets [] notation. However, functions are callable objects. Therefore, they require parentheses (), which are sometimes also called round brackets.

Let's look at an example where Python raises this error:

numbers = [2, 4, 6, 8]
numbers.append[10]
Traceback (most recent call last):
  ...
TypeError: 'builtin_function_or_method' object is not subscriptable

The following section explores this error in more detail.

Understanding the Error

Python functions are callable objects, which means we can use parentheses after the function name to execute the code in the function definition. One of the most common examples of a built-in function in Python is print():

print("This is a built-in function")
This is a built-in function

Parentheses are used to call a function in Python. However, using square brackets instead of parentheses raises an error:

print["This is a built-in function"]
Traceback (most recent call last):
  ...
TypeError: 'builtin_function_or_method' object is not subscriptable

Let's break the error message down into its key parts:

  • A 'builtin_function_or_method' object
  • Not subscriptable

In the example above, print() is a built-in function. The description also refers to methods, which are functions that are part of a class. Python has many built-in methods associated with its data types. Let's look at another example that raises this error message:

name = "DataCamp"
print(name.count["a"])
Traceback (most recent call last):
  ...
TypeError: 'builtin_function_or_method' object is not subscriptable

The .count() method, like functions in general, needs parentheses:

print(name.count("a"))
3

This scenario is similar to the example in the introduction, which contains the list method .append(). Here's the correct version using parentheses:

numbers = [2, 4, 6, 8]
numbers.append(10)
print(numbers)
[2, 4, 6, 8, 10]

TypeError: 'function' object is not subscriptable

Python raises a similar error with user-defined functions:

def greet_person(name):
    print(f"Hello {person}")
   
greet_person["James"]
Traceback (most recent call last):
  ...
TypeError: 'function' object is not subscriptable

However, the function greet_person() is not one of the built-in functions in Python. The error message states this is a 'function' rather than a 'builtin_function_or_method'.

The second part of the error message refers to the term 'subscriptable'. A Python object is subscriptable if we can use square brackets to access one of its elements. A common example of a subscriptable object is a sequence, which can be indexed using square brackets:

print(numbers[0])
print(numbers[-1])
print(numbers[1:4])
2
10
[4, 6, 8]

Tuples and strings are other examples of sequences. Therefore, they're also subscriptable:

more_numbers = 10, 30, 50
print(more_numbers[0])
name = "DataCamp"
print(name[0])
10
'D'

Python mappings are also subscriptable. The most common mapping in Python is the dictionary:

points = {"James": 10, "Mary": 14}
print(points["Mary"])
14

Dictionary keys are used instead of indices within the square brackets.

Python: Callable vs. Subscriptable

Sequences and mappings are examples of subscriptable Python objects. We can use square brackets after the object's name to access an object from within the data structure. A Python class creates subscriptable objects if it defines the .__getitem__() special method.

Functions and methods are examples of callable objects. We can use parentheses after the object's name to call them. Calling a function or method executes the code in the function definition.

Classes are another callable Python object. Calling a class creates an instance. In general, a Python object is callable if its class defines the .__call__() special method.

Python raises a similar error message when we try to call an object that's not callable:

numbers = [2, 4, 6, 8]
print(numbers(0))
Traceback (most recent call last):
  ...
TypeError: 'list' object is not callable

The variable numbers is a list, which isn't callable. The correct way to access the first item from the list is to use square brackets: numbers[0]. Functions, methods, and classes are the most common callable objects in Python.

Fix 'builtin_function_or_method' object is not subscriptable`

Python raises the error message ``'builtin_function_or_method' object is not subscriptable` when a function or method is followed by square brackets. Look out for square brackets right after the function or method name and replace these with parentheses.

In some instances, a function or method has a list as an argument, which is defined using square brackets. However, note that the list is defined within parentheses:

number_of_values = len([10, 30, 50])
print(number_of_values)
numbers = [2, 4, 6, 8]
numbers.extend([10, 12])
print(numbers)
3
[2, 4, 6, 8, 10, 12]

In these examples, the function or method name is followed by parentheses. The argument within the parentheses is a list. The square brackets notation to define a list differs from the square brackets notation used with sequences, mappings, and other subscriptable objects.

Debugging Tips

One thing you can do is checking the object type using the built-in type() function:

print(type(print))
numbers = [2, 4, 6, 8]
print(type(numbers.append))
<class 'builtin_function_or_method'>

The output confirms that the object is a built-in function or method, which is callable. There's also a built-in function called callable() that returns True if its argument can be called:

print(callable(numbers.append))
True

Other debugging tips to consider:

  • Check the type of brackets or parentheses. Callables, such as functions and methods, need to be followed by parentheses. Square brackets are used for sequences, mappings, and other subscriptable objects.
  • Look for clues in the error message referring to built-in functions or methods and subscriptable objects. The line number in the error messages points to the line containing the error.

Conclusion

Programming languages like Python use different marks for distinct purposes. Square brackets are used immediately after an object name to access an element from the data structure. This square bracket notation works on subscriptable objects. Square brackets are also used to create lists when they don't follow immediately after an object name.

Parentheses are used after an object name to call the object. Functions and methods are examples of callable objects in Python. Parentheses are also associated with tuples in Python, and they're used to group parts of a statement together when necessary.

Python raises the exception 'builtin_function_or_method' object is not subscriptable when a built-in function or method is followed by square brackets. The correct way to call a function or method is to use parentheses.

You can read more about functions in DataCamp's Python Functions: How to Call & Write Functions.


Stephen Gruppetta's photo
Author
Stephen Gruppetta
LinkedIn
Twitter

I studied Physics and Mathematics at UG level at the University of Malta. Then, I moved to London and got my PhD in Physics from Imperial College. I worked on novel optical techniques to image the human retina. Now, I focus on writing about Python, communicating about Python, and teaching Python.

Topics

Learn Python with these courses!

course

Introduction to Python

4 hr
5.7M
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

tutorial

Python Functions: How to Call & Write Functions

Discover how to write reusable and efficient Python functions. Master parameters, return statements, and advanced topics like lambda functions. Organize your code better with main() and other best practices.
Karlijn Willems's photo

Karlijn Willems

14 min

tutorial

Troubleshooting the No module named 'sklearn' Error Message in Python

Learn how to quickly fix the ModuleNotFoundError: No module named 'sklearn' exception with our detailed, easy-to-follow online guide.
Amberle McKee's photo

Amberle McKee

5 min

tutorial

Python lambda Tutorial

Learn a quicker way of writing functions on the fly with lambda functions.
DataCamp Team's photo

DataCamp Team

3 min

tutorial

Python KeyError Exceptions and How to Fix Them

Learn key techniques such as exception handling and error prevention to handle the KeyError exception in Python effectively.
Javier Canales Luna's photo

Javier Canales Luna

6 min

tutorial

Introducing Python Magic Methods

Discover what magic methods is while looking at the difference between methods and functions.
Kurtis Pykes 's photo

Kurtis Pykes

11 min

tutorial

Python List Functions & Methods Tutorial and Examples

Learn about Python List functions and methods. Follow code examples for list() and other Python functions and methods now!
Abid Ali Awan's photo

Abid Ali Awan

7 min

See MoreSee More