Python TypeError: ‘function’ object is not subscriptable Solution

Posted in /  

Python TypeError: ‘function’ object is not subscriptable Solution
vinaykhatri

Vinay Khatri
Last updated on November 9, 2024

    In Python, to call a function, we use the function name followed by parenthesis () , and pass the argument values inside the parenthesis separated by commas. But if we try to call a function using a square bracket instead of parenthesis, we will receive the Error TypeError: 'function' object is not subscriptable .

    In this Python guide, we will walk through this error and discuss why this error occurs in Python and how to debug it. We will also discuss a common example scenario where many Python learners commit mistakes and encounter this error.

    Let's get started with the error statement.

    Python Error:TypeError: 'function' object is not subscriptable

    The error statement TypeError: 'function' object is not subscriptable has two parts Exception Type ( TypeError ) and Error Message ( 'function' object is not subscriptable)

    TypeError (Exception type)

    TypeError is one of the standard Python exceptions. It occurs in a python program when we try to perform an operation on an invalid operation on a Python data object.

    'function' object is not subscriptable (Error Message)

    This error message is telling us that we are performing the subscript or indexing operation on a function object. In Python, everything is an object, including the function, and when we try to perform the indexing operation on a function, we receive this error message.

    Example

    # function to square numbers
    def square(a):
        return a*a
    
    a = 20
    
    print(f"Square of {a}:", square[a])

    Output

    Traceback (most recent call last):
      File "main.py", line 7, in 
        print(f"Square of {a}:", square[a])
    TypeError: 'function' object is not subscriptable

    Break the Code

    In this example, we are getting the error in line 7 , with print(f"Square of {a}:", square[a]) statement. If we read the error carefully, we can tell that the error is something related to the 'function' object and subscriptable . And when we review the code statement, we will find out that we are calling the square function as a subscriptable object (list, string, or tuple) using square brackets [] , instead of parenthesis () .

    Solution

    To solve or debug the above example, all we need to do is, change the square bracket with parenthesis, so the Python interpreter treats the square as a function call, not as a subscriptable object.

    # function to square numbers
    def square(a):
        return a*a
    
    a = 20
    
    print(f"Square of {a}:", square(a))

    Output

    Square of 20: 400

    Common Example Scenario

    The most common mistake when many Python learners encounter this error is when they use the square brackets instead of parenthesis while calling a function. Function calling uses parenthesis after the function name, and indexing uses a square bracket after the list, tuple, or string name.

    But if we put the indexing's square bracket [] after a function name, the Python interpreter would try to perform the indexing operation on the function object, and when it finds out that there is no such operation supported by a Python function object, it will throw the error.

    Example

    # function to add two numbers
    def add(a,b):
        return a+b
    
    a = 20
    b=30
    
    print(f"The sum of {a}+{b}:", add[a,b])

    Output

    Traceback (most recent call last):
      File "main.py", line 8, in 
        print(f"The sum of {a}+{b}:", add[a,b])
    TypeError: 'function' object is not subscriptable

    Solution

    To solve the above problem, we need to replace the square brackets after the add name by parenthesis.

    # function to add two numbers
    def add(a,b):
        return a+b
    
    a = 20
    b=30
    
    print(f"The sum of {a}+{b}:", add(a,b))

    Output

    The sum of 20+30: 50

    Conclusion

    Now let's conclude this article on "Python TypeError: ‘function’ object is not subscriptable Solution". In this article, we discussed why the following error occurs in a Python program and how to solve it. There are many cases in Python when a TypeError exception occurs, but the error message 'function' object is not subscriptable only raises when we try to call a function using square brackets.

    If you are getting this error in your python program, you can share your code in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments