TypeError: 'method' object is not subscriptable” occurs when you call a method using square brackets, e.g.,
object.method. To fix it, call the non-subscriptable method only with parentheses like so:
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
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)
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) 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
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]
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) instead of
The following code shows how to correctly access the methods
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:
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) # 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)
Also, feel free to join our free email academy — it’s fun and we have cheat sheets!
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.