Python TypeError: 'method' object is not subscriptable Solution

Posted in /  

Python TypeError: 'method' object is not subscriptable Solution
vinaykhatri

Vinay Khatri
Last updated on March 29, 2024

    A method is a function defined inside a Python class. Similar to  the function call, we use parenthesis to call a method, but the difference is we call a method on an object using the dot . operator. While calling a method, if we use a square bracket [] instead of parenthesis () , we encounter the TypeError: 'method' object is not subscriptable Error.

    In this Python tutorial, we will learn why TypeError: 'method' object is not subscriptable Error occurs in a Python program and how to solve it.

    Python Problem: TypeError: 'method' object is not subscriptable

    The error statement TypeError: 'method' object is not subscriptable has two parts.

    1. TypeError
    2. 'method' object is not subscriptable

    1. TypeError

    TypeError is raised in a Python program when we try to perform an unsupportable operation on an object or pass an invalid data type to a function.

    2. 'method' object is not subscriptable

    This is the error message which is telling us that the method is not a subscriptable object and can not use indexing. This error message is raised in a Python program when we use the square bracket [] to call a method instead of a parenthesis () .

    Example

    class pizza():
        def __init__(self,size):
            self.size = size
    
        def add_toppings(self, topping_list):
            self.add_toppings = topping_list
    
            print("Your Added Toppings are:")
            for topping in self.add_toppings:
                print(topping)
    
    pizza_size = 'medium'
    
    # create the class object
    my_pizza = pizza(pizza_size)
    
    my_toppings = ['mushrooms', 'onions', 'black olivs']
    
    # call the add topping method using square bracket
    my_pizza.add_toppings[my_toppings]   #error

    Output

    Traceback (most recent call last):
      File "main.py", line 20, in
         my_pizza.add_toppings(my_toppings)   #error
    TypeError: 'method' object is not subscriptable

    In this example, we are getting this error because we used the square bracket to call the add_toppings method.

    Solution

    To solve the above problem, all we need to do is change the square brackets with parenthesis.

    class pizza():
        def __init__(self,size):
            self.size = size
    
        def add_toppings(self, topping_list):
            self.add_toppings = topping_list
    
            print("Your Added Toppings are:")
            for topping in self.add_toppings:
                print(topping)
    
    pizza_size = 'medium'
    
    # create the class object
    my_pizza = pizza(pizza_size)
    
    my_toppings = ['mushrooms', 'onions', 'black olivs']
    
    # call the add topping method using parentheses  bracket
    my_pizza.add_toppings(my_toppings)   #solved

    Conclusion

    In this Python tutorial, we learned what is TypeError: 'method' object is not subscriptable error in Python and how to solve it. This error occurs in Python when we use the square brackets to call a class method. Class methods are similar to the function, and to call them, we need to use parenthesis after the method name.

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

    People are also reading:

    Leave a Comment on this Post

    0 Comments