Arguments in Python methods must be specified within parentheses. This is because functions and methods both use parentheses to tell if they are being called. If you use square brackets to call a method, you’ll encounter an “TypeError: ‘method’ object is not subscriptable” error.
In this guide, we discuss what this error means and why you may encounter it. We walk through an example of this error to help you develop a solution.
TypeError: ‘method’ object is not subscriptable
Subscriptable objects are objects with a __getitem__ method. These are data types such as lists, dictionaries, and tuples. The __getitem__ method allows the Python interpreter to retrieve an individual item from a collection.
Not all objects are subscriptable. Methods, for instance, are not. This is because they do not implement the __getitem__ method. This means you cannot use square bracket syntax to access the items in a method or to call a method.
Consider the following code snippet:
cheeses = ["Edam", "Stilton", "English Cheddar", "Parmesan"] print(cheeses[0])
This code returns “Edam”, the cheese at the index position 0. We cannot use square brackets to call a function or a method because functions and methods are not subscriptable objects.
An Example Scenario
Here, we build a program that stores cheeses in objects. The “Cheese” class that we use to define a cheese will have a method that lets us check whether a cheese is from a particular country of origin.
Start by defining a class for our cheeses. We call this class Cheese:
class Cheese: def __init__(self, name, origin): self.name = name self.origin = origin def get_country(self, to_compare): if to_compare == self.origin: print("{} is from {}.".format(self.name, self.origin)) else: print("{} is not from {}. It is from {}.".format(self.name, to_compare, self.origin))
Our class contains two methods. The first method defines the structure of the Cheese object. The second lets us check whether the country of origin of a cheese is equal to a particular value.
Next, we create an object from our Cheese class:
edam = Cheese("Edam", "Netherlands")
The variable “edam” is an object. The name associated with the cheese is Edam and its country of origin is the Netherlands.
Next, let’s call our get_country()
method:
edam.get_country["Germany"]
This code executes the get_country()
method from the Cheese class. The get_country()
method checks whether the value of “origin” in our “edam” object is equal to “Germany”.
Run our code and see what happens:
Traceback (most recent call last): File "main.py", line 14, in <module> edam.get_country["Germany"] TypeError: 'method' object is not subscriptable
An error occurs in our code.
The Solution
Let’s analyze the line of code that the Python debugger has identified as erroneous:
edam.get_country["Germany"]
In this line of code, we use square brackets to call the get_country()
method. This is not acceptable syntax because square brackets are used to access items from a list. Because functions and objects are not subscriptable, we cannot use square brackets to call them.
To solve this error, we must replace the square brackets with curly brackets:
edam.get_country("Germany")
Let’s run our code and see what happens:
Edam is not from Germany. It is from Netherlands.
Our code successfully executes. Let’s try to check whether Edam is from “Netherlands” to make sure our function works in all cases, whether or not the value we specify is equal to the cheese’s origin country:
edam.get_country("Netherlands")
Our code returns:
Edam is from Netherlands.
Our code works if the value we specify is equal to the country of origin of a cheese.
Conclusion
The “TypeError: ‘method’ object is not subscriptable” error is raised when you use square brackets to call a method inside a class. To solve this error, make sure that you only call methods of a class using curly brackets after the name of the method you want to call.
Now you’re ready to solve this common Python error like a professional coder!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.