How to fix TypeError: 'method' object is not subscriptable

One error that you might encounter when working with Python is:

TypeError: 'method' object is not subscriptable

This error occurs when you mistakenly call a class method using the square [] brackets instead of round () brackets.

The error is similar to the [‘builtin_function_or_method’ object is not subscriptable]({{ <ref “06-python-builtinfunctionormethod-object-is-not-subscriptable”>}}) in nature, except the error source is from a method you defined instead of built-in Python methods.

Let’s see an example that causes this error and how to fix it.

How to reproduce this error

Suppose you wrote a Python class in your code with a method defined in that class:

class Human:
    def talk(self, message):
        print(message)

Next, you create an instance of that class and call the talk() method, but instead of using parentheses, you used square brackets as shown below:

class Human:
    def talk(self, message):
        print(message)

person = Human()

person.talk["How are you?"]

The square brackets around talk["How are you?"] will cause an error:

Traceback (most recent call last):
  File "main.py", line 8, in <module>
    person.talk["How are you?"]
TypeError: 'method' object is not subscriptable

This is because Python thinks you’re trying to access an item from the talk variable instead of calling it as a method.

To resolve this error, you need to put parentheses around the string argument as follows:

person = Human()

person.talk("How are you?")  # ✅

By replacing the square brackets with parentheses, the function can be called and the error is resolved.

Sometimes, you get this error when you pass a list to a function without adding the parentheses.

Suppose the Human class has another method named greet_friends() which accepts a list of strings as follows:

class Human:
    def greet_friends(self, friends_list):
        for name in friends_list:
            print(f"Hi, {name}!")

Next, you create an instance of Human and call the greet_friends() method:

person = Human()

person.greet_friends['Lisa', 'John', 'Amy']  # ❌

Oops! The greet_friends method lacked parentheses, so you get the same error:

Traceback (most recent call last):
  File "main.py", line 8, in <module>
    person.greet_friends['Lisa', 'John', 'Amy']
TypeError: 'method' object is not subscriptable

Even when you’re passing a list to a method, you need to surround the list using parentheses as follows:

person.greet_friends( ['Lisa', 'John', 'Amy'] )  # ✅

By adding the parentheses, the error is now resolved and the person is able to greet those friends:

Hi, Lisa!
Hi, John!
Hi, Amy!

Conclusion

The TypeError: ‘method’ object is not subscriptable occurs when you call a class method using the square brackets instead of parentheses.

To resolve this error, you need to add parentheses after the method name. This rule applies even when you pass a list to the method as shown in the examples.

I hope this tutorial is helpful. See you in other tutorials! 👍

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.