TypeError: ‘function’ object is not subscriptable
The error message TypeError: ‘function’ object is not subscriptable occurs while working with functions in Python. In this article, we will understand what this error means, the reasons behind this error, and the different scenarios that can cause this error. Also, we will explore different ways to solve this error.
“TypeError: ‘function’ object is not subscriptable” is a common error that occurs when attempting to access a function using square brackets as if it were a list or dictionary. To fix it, use square brackets only with data structures, not functions. Modify the function to return the value or character you want to access instead.

What does TypeError: ‘function’ object is not subscriptable mean?
First, let’s understand what is the meaning of subscriptable in Python. The term subscriptable refers to those objects that can contain other objects, you can think of it as a collection or container. Specifically, a subscriptable object is one that implements the __getitem__() method, which can use square brackets notation to retrieve its elements. Some subscriptable objects in Python are Dictionaries, Lists, Strings, and Tuples.
However, Python functions are not subscriptable objects which means it does not support subscriptable operators (square brackets) to access its elements. The error TypeError: ‘function’ object is not subscriptable itself explains that functions are not subscriptable objects which means we cannot call it using square bracket notation.
Why this error occurs in Python?
There are various reasons which can cause this error. It usually occurs when someone tries to access function arguments using square bracket[] notation which is not supported instead of round brackets(). Let’s see different scenarios which can raise this error.
Scenario Example: Using the Square Bracket Notation to Call Function
Consider a scenario where you want to create a function greeting_func() that will greet a person whose name will be passed as an argument. For example:
# function definition def greeting_msg(name): print("Good Morning!", name) # calling function greeting_msg["Richard"]
Output
TypeError: 'function' object is not subscriptable
Oops! an error is encountered, let’s see why. In this example, the greeting_msg() function is defined that accepting one argument name to print a greeting message. As can observe, on the execution of the above code TypeError: ‘function’ object is not subscriptable error occurred. Because we are calling the greeting_msg() function with square brackets instead of parenthesis or round brackets ().
Solution:
Check the below example to see how to fix the above error:
# function definition def greeting_msg(name): print("Good Morning!", name) # calling function greeting_msg("Richard")
Try it yourself – Online Python editor to check the examples
Output
Good Morning! Richard
You can see, the error TypeError: ‘function’ object is not subscriptable and is removed now, and the greeting message Good Morning! Richard is printed on the console. To fix this error the function greeting_msg() is called using parentheses () to pass the name argument to a function.
Scenario Example: Having Function Name Same as Variable Name
greeting_msgList = ['Good Morning!', 'Good Night!', 'Good Noon!'] def greeting_msgList(): return ['Good Morning!', 'Good Night!', 'Good Noon!'] greeting_msgList[2] # calling list
Output
TypeError: 'function' object is not subscriptable
In this example, the error is caused by a naming conflict between the variable and the function. Since they both have the same name greeting_msgList, the Python interpreter is unable to distinguish between them. We can see, the function greeting_msgList() overrides the list.
So, when the code tries to access the second element of the list using greeting_msgList[2], the interpreter assumes that greeting_msgList refers to the function instead of the variable, resulting in the error.
Solution:
To avoid this issue, choose unique and descriptive names for variables and functions that do not clash with each other. As shown in the below example:
greetings_list = ['Good Morning!', 'Good Night!', 'Good Noon!'] def get_greetings(): return ['Good Morning!', 'Good Night!', 'Good Noon!'] print(greetings_list[2]) # calling list
Try it yourself – Online Python editor to check the examples
Output
Good Noon!
On execution of the above example, the second element of the list Good Noon! is successfully printed on the console. Because now the variable greetings_list has a descriptive name that clearly distinguishes it from the function get_greetings().
Conclusion:
In this article, we have discussed the TypeError: ‘function’ object is not subscriptable error in Python, which occurs when you try to use square bracket [] notation to access elements of a function object. We have explored the causes of this error, which include attempting to call a function using square brackets and naming conflict between the variable and the function. We have also discussed how to resolve this error, which involves revising your code to use parentheses to call a function and using the descriptive variable name that clearly distinguishes it from the function name. I hope that by using the solutions provided in this article you will resolve this error.
Good Luck with your Learning!!