How to Resolve "TypeError: 'method' object is not subscriptable" in Python
When working with classes in Python, you might encounter the TypeError: 'method' object is not subscriptable'. This error is a clear signal that you are using square brackets [] to try to call a method, when you should be using parentheses (). The square bracket syntax is for "subscripting"—accessing elements from a container—while parentheses are for "calling" or executing a function or method.
This guide will explain the difference between callable and subscriptable objects, show you the common mistakes that trigger this error, and provide the simple but crucial syntax correction to fix it.
Understanding the Error: "Calling" vs. "Subscripting"
To understand this error, it's essential to distinguish between two fundamental operations in Python:
- Callable: An object that can be "called" or executed using parentheses
(). This category includes functions and methods. For example,my_function()ormy_object.my_method(). - Subscriptable: An object that can have its elements accessed using square brackets
[]. This category includes sequences like lists, tuples, strings, and mapping types like dictionaries. For example,my_list[0]ormy_dict['key'].
The TypeError: 'method' object is not subscriptable is Python's way of telling you that you've tried to perform a subscripting operation [] on a method, which is a callable object, not a container.
Scenario 1: Using [] Instead of () for a Simple Method Call
This is the most straightforward cause of the error. You define a method and then mistakenly use [] when you intend to call it.
Example of code causing the error:
class Human:
def talk(self, message):
print(message)
person = Human()
# Incorrect: Using square brackets attempts to get an item from the 'talk' method.
person.talk["How are you?"]
Output:
Traceback (most recent call last):
File "main.py", line 8, in <module>
person.talk["How are you?"]
TypeError: 'method' object is not subscriptable
Solution: to call the method, you must use parentheses ().
class Human:
def talk(self, message):
print(message)
person = Human()
# ✅ Correct: Use parentheses to call the method and pass the argument.
person.talk("How are you?")
Output:
How are you?
Scenario 2: Incorrectly Passing a List Argument
The confusion between [] and () can also happen when you are passing a list as an argument to a method.
Example of code causing the error:
class Human:
def greet_friends(self, friends_list):
for name in friends_list:
print(f"Hi, {name}!")
person = Human()
# Incorrect: This syntax tries to get an item from the 'greet_friends' method.
person.greet_friends['Lisa', 'John', 'Amy']
Output:
Traceback (most recent call last):
File "main.py", line 10, in <module>
person.greet_friends['Lisa', 'John', 'Amy']
TypeError: 'method' object is not subscriptable
In the incorrect example, Python interprets 'Lisa', 'John', 'Amy' as a tuple ('Lisa', 'John', 'Amy'). The code is effectively trying to do person.greet_friends[('Lisa', 'John', 'Amy')], which is invalid.
Solution: even when the argument itself is a list (which uses [] in its own syntax), you must still call the method using parentheses (). The list is passed inside these parentheses.
class Human:
def greet_friends(self, friends_list):
for name in friends_list:
print(f"Hi, {name}!")
person = Human()
# ✅ Correct: The list is an argument passed inside the call parentheses ().
person.greet_friends(['Lisa', 'John', 'Amy'])
Output:
Hi, Lisa!
Hi, John!
Hi, Amy!
Conclusion
The TypeError: 'method' object is not subscriptable is a fundamental syntax error that arises from confusing the operators for calling a method and accessing an item from a container.
| Syntax | Purpose | Example |
|---|---|---|
() | Calling a method or function. | my_method(argument) |
[] | Accessing an item (subscripting) in a container. | my_list[index] or my_dict[key] |
To fix this error, always ensure you are using parentheses () to execute a method, even if the argument you are passing to it is a list.