Typeerror: method object is not subscriptable

Are you struggling to fix this “typeerror: method object is not subscriptable error message”?

Therefore, keep on reading, as we are going to show you how you can fix it with simple yet effective solutions.

Aside from that, in this article, you’ll have a better understanding of “method object is not subscriptable.”

What is “typeerror: method object is not subscriptable”?

The “typeerror: ‘method’ object is not subscriptable” is an error message in Python.

That is usually raised when you use square brackets [] to call a method instead of parenthesis or round brackets ().

In addition to that, this error message indicates that you are trying to use the square bracket [] syntax to access the method object, which is not allowed.

A method in Python, is a function that is associated with an object, and cannot be accessed directly with square brackets [].

How to fix “typeerror: method object is not subscriptable”?

To fix this error, you have to make sure that you are using the correct syntax.

By replacing the square brackets [] with round brackets () after the method’s name when you are calling it.

For instance, if you have a method called sample_list and you are calling it like this: sample_list. append [4], you should change it to sample_list.append(4).

Incorrect code:


sample_list = [1, 2, 3]
result = sample_list.append[4] # This line will produce the error

Correct code


sample_list = [1, 2, 3]
result = sample_list.append(4) # This line will not produce the error

Here are the following solutions you may use to resolve this ‘”method’ object is not subscriptable” type error:

1: Using parentheses to call the method

If you’re trying to access the method object using square brackets.

You can fix the error by simply calling the method with any required arguments instead.

sample_list = [1, 2, 3, 4, 5]
sample_method = sample_list.append
sample_method(6)
print(sample_list[2])

Output:

3

2: Use call() method

The alternative way to call a method object is to use its call() method.

my_list = [10, 20, 30, 40, 50]
my_method = my_list.append
my_method.__call__(6)
print(my_list[2])

Output:

30

3: Use methods get() and append_all()

Here’s the example code that shows how to access the methods get() and append_all() correctly using a custom list:

class SampleList:

    def __init__(self, lst):
        self.lst = lst

    def get(self, index):
        return self.lst[index]

    def append_all(self, lst):
        self.lst.extend(lst)


sample_list = SampleList(['Itsourcecode'])
sample_list.append_all(['sourcecodehero', 'ITSC'])
print(sample_list.get(1))

Output:

sourcecodehero

Conclusion

By using the provided solutions above, you can fix the “typeerror: method object is not subscriptable” error and access the method object correctly.

To avoid this error, simply do not call methods using the square bracket [].

We are hoping that this article provided you with sufficient solutions to get rid of the error.

You could also check out other “typeerror” articles that may help you in the future if you encounter them.

Thank you very much for reading to the end of this article.