(Solved) Python TypeError ‘Method’ Object is Not Subscriptable

The β€œTypeError: 'method' object is not subscriptable” occurs when you call a method using square brackets, e.g., object.method[3]. To fix it, call the non-subscriptable method only with parentheses like so: object.method(3).

How Does the Error Occur (Example)

In the following minimal example, you attempt to define a custom “list” class and you try to access the list element at positon 1 by using the method call my_list.get[1]. ⚑

class MyList:

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

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


my_list = MyList(['Alice', 'Bob', 'Carl'])
print(my_list.get[1])

However, Python raises the following TypeError: 'method' object is not subscriptable:

Traceback (most recent call last):
  File "C:\Users\xcent\Desktop\code.py", line 11, in <module>
    print(my_list.get[1])
TypeError: 'method' object is not subscriptable

The reason is that Python thinks you are trying to access the my_list.get method object at position 1. However, even though a method is an object in Python too, this is not what you want to do. You want to access the position 1 of the list inside the my_list object!

An example follows in a minute! ‡️

A similar error would occur for any of the following indexing or slicing calls:

  • my_list.get[1, 2, 3]
  • my_list.get[1:3]
  • my_list.get[:]
  • my_list.get[-1]

Again, you cannot call methods the same way you would call lists, i.e., using the square bracket notation.

How to Resolve the Error (Easy) + Example

To fix this error in the easiest possible way, simply do not call methods using the square bracket notation. Only use the square bracket notation on subscriptable list or sequence objects (or objects that define the __getitem__() method, see below).

In many cases, this is simply a matter of using the normal parentheses that was missed. So, use object.method([1]) or object.method(1) instead of object.method[1].

The following code shows how to correctly access the methods get() and append_all() using our custom list example:

class MyList:

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

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

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


my_list = MyList(['Alice'])
my_list.append_all(['Bob', 'Carl'])
print(my_list.get(-1))
# Carl

Both method calls now work beautifully. ❀️

Stay with me for a moment because there is actually a very Pythonic concept that you should know. It’s called “magic methods” or “dunder methods”. πŸ‘‡

How to Resolve the Error (Advanced) + Example

In some cases, you may actually want to make the class subscriptable. I show you how to do this just for fun and to help you improve your skills. πŸ‘

ℹ️ Python raises the TypeError object is not subscriptable if you use indexing with the square bracket notation on an object that is not indexable. This is the case if the object doesn’t define the __getitem__() dunder method.

Here’s a minimal example that defines the dunder method and, thereby, making the custom class MyList subscriptable so you can use the square bracket notation like so: MyList[0].

class MyList:

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

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


my_list = MyList(['Alice', 'Bob', 'Carl'])

print(my_list[0])
# Alice

Yay! This way, you can actually apply the square bracket notation to your custom class!

Feel free to read on about this topic in our detailed guide here:

πŸ‘‰ Recommended Tutorial: Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug)

Join Us

Also, feel free to join our free email academy — it’s fun and we have cheat sheets!