How to Solve Python TypeError: ‘method’ object is not subscriptable

by | Programming, Python, Tips

When calling a method in Python, you have to use parentheses (). If you use square brackets [], you will raise the error “TypeError: ‘method’ object is not subscriptable”.

This tutorial will describe in detail what the error means. We will explore an example scenario that raises the error and learn how to solve it.

TypeError: ‘method’ object is not subscriptable

TypeErrror occurs when you attempt to perform an illegal operation for a particular data type. The part “‘method’ object is not subscriptable” tells us that method is not a subscriptable object. Subscriptable objects have a __getitem__ method, and we can use __getitem__ to retrieve individual items from a collection of objects contained by a subscriptable object. Examples of subscriptable objects are lists, dictionaries and tuples. We use square bracket syntax to access items in a subscriptable object. Because methods are not subscriptable, we cannot use square syntax to access the items in a method or call a method.

Methods are not the only non-subscriptable object. Other common “not subscriptable” errors are:

Let’s look at an example of retrieving the first element of a list using the square bracket syntax

pizzas = ["margherita", "pepperoni", "four cheeses", "ham and pineapple"]
print(pizzas[0])

The code returns:

margherita

which is the pizza at index position 0. Let’s look at an example of calling a method using square brackets.

Example: Calling A Method With Square Brackets

Let’s create a program that stores fundamental particles as objects. The Particle class will tell us the mass of a particle and its charge. Let’s create a class for particles.

class Particle:

         def __init__(self, name, mass):

             self.name = name

             self.mass = mass

         def is_mass(self, compare_mass):

             if compare_mass != self.mass:

                 print(f'The {self.name} mass is not equal to {compare_mass} MeV, it is {self.mass} MeV')

             else:

                 print(f'The {self.name} mass is equal to {compare_mass} MeV')

The Particle class has two methods, one to define the structure of the Particle object and another to check if the mass of the particle is equal to a particular value. This class could be useful for someone studying Physics and particle masses for calculations.

Let’s create a muon object with the Particle class. The mass is in MeV and to one decimal place.

muon = Particle("muon", 105.7)

The muon variable is an object with the name muon and mass value 105.7. Let’s see what happens when we call the is_mass() method with square brackets and input a mass value.

muon.is_mass[105.7]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [18], in <cell line: 1>()
----> 1 muon.is_mass[105.7]

TypeError: 'method' object is not subscriptable

We raise the TypeError because of the square brackets used to call the is_mass() method. The square brackets are only suitable for accessing items from a list, a subscriptable object. Methods are not subscriptable; we cannot use square brackets when calling this method.

Solution

We replace the square brackets with the round brackets ().

muon = Particle("muon", 105.7)

Let’s call the is_mass method with the correct brackets.

muon.is_mass(105.7)

muon.is_mass(0.51)
The muon mass is equal to 105.7 MeV

The muon mass is not equal to 0.51 MeV, it is 105.7 MeV

The code tells us the mass is equal to 105.7 MeV but is not equal to the mass of the electron 0.51 MeV.

To learn more about correct class object instantiation and calling methods in Python go to the article titled “How to Solve Python missing 1 required positional argument: ‘self’“.

Summary

Congratulations on reading to the end of this tutorial! The error “TypeError: ‘method’ object is not subscriptable” occurs when you use square brackets to call a method. Methods are not subscriptable objects and therefore cannot be accessed like a list with square brackets. To solve this error, replace the square brackets with the round brackets after the method’s name when you are calling it.

To learn more about Python for data science and machine learning, go to the online courses page on Python for the best courses available!

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨