'method' Object Is Not Subscriptable - Python Error

'method' Object Is Not Subscriptable - Python Error

Python throws error 'method' object is not subscriptable when a class method is indexed with [] like it its a list or array.

Python throws error, ‘method’ object is not subscriptable, when a class method is indexed or subscripted using square brackets [] like if its a list or tuple or array. Consider this example –

class SuperHeroList:
    def __init__(self):
        self.theSuperHeroList = list()
    def __getitem__(self, i):
       print(self.theSuperHeroList[i])
    def insert(self, lst):
        for x in lst:
            try:
                self.theSuperHeroList.append(str(x))
            except:
                print("oops")

myList = SuperHeroList()
myList.insert["Captain America", "Hulk", "Thor"]

PythonCopy

This code will throw the error that method is not subscriptable. In our SuperHeroList class we have defined a method insert(), which is accepting a list as argument and appending it with internal list, theSuperHeroList.

python error python error python-short

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

top 30 Python Tips and Tricks for Beginners

In this post, we'll learn top 30 Python Tips and Tricks for Beginners

Lambda, Map, Filter functions in python

You can learn how to use Lambda,Map,Filter function in python with Advance code examples. Please read this article

'function' Object Is Not Subscriptable - Python Error

Python throws error, 'function' object is not subscriptable, when we try to index or subscript a function. Lear from code and demo.

'float' Object Is Not Iterable - Python Error

Python throws error, 'float' object is not iterable, when you try to loop over a float value. Floats are not an iterable object.

‘dict’ Object Has No Attribute ‘iteritems’, ‘iterkeys’ Or ‘itervalues’ – Python Error

Python throws error, ‘dict’ object has no attribute ‘iteritems’, because `iteritems()` function is removed from Python 3\. Similarly, functions like `iterkeys()` and `itervalues()` are also removed. According to [Python3.0 Built-in changes...