Python

_int_ Object is not Scriptable

A subscriptable object in Python refers to objects that act as containers. Or, the object contains other objects. You can use an indexing notation to access specific elements of the object. An object is subscriptable if it implements the __getitem__() or __class_getitem__() methods.

This article discussed the Python unsubscriptable error, what causes it, and how to fix it.

What causes the ‘in’ object is not Subscriptable Error?

This error is caused when you treat an integer type as a subscribable object in Python.

This means that you are attempting to access elements of an int type that is not supported.

An example is as shown:

i = 1000
print(i[0])

In this case, we are attempting to access an element of an integer type that is not supported for an int object is not subscriptable.

Running the code above should return an error as shown:

$ python subscriptable.py
Traceback (most recent call last):
  File " subscriptable.py", line 2, in
    print(i[0])
TypeError: 'int' object is not subscriptable

This prevents us from using indexing notations to access elements of an int type.

In Python, subscriptable objects include:

  1. string
  2. tuple
  3. list
  4. dict
  5. any class that implements __getitem__()

How to Fix TypeError: ‘int’ object is not subscriptable Error

To fix this type of error, you should avoid using indexing notation on an unsupported type.

However, if you need to access elements of an object, you can convert it into a subscriptable object.

For example, in our previous example, we can convert the integer into a string type as its subscriptable.

Consider the illustration shown below:

i = 1000
i = str(i)
print(i[0])

In this case, the code should work and return the element at index 0 as shown:

$ python subscriptable.py
1

Bonus: Implementing __getitem__() in your class

In some cases, you may need your object to be subscriptable. Therefore, you need to implement the __getitem__ method in your class.

The example below shows how yo create a simple subscriptable object.

class MyClass:
    def __getitem__(self, key):
        return key
new_object = MyClass()
print(new_object[0])

In the example above, we start by creating a simple class that implements the __getitem__ method.

The function will simply take the key and return itself. For example, running the code above should return:

$ python subscriptable.py
0

You can use this notation to get a specific element of your object.

An example is as shown:

class Car:
    def __init__(self, model):
        self.model = model
    def __getitem__(self, key):
        return self.model[key]
y = Car('Honda Civic')
print(y[0:5])

In this case, the method allows you to fetch elements at a specific index. Running the code above should return:

$ python subscriptable.py
Honda

You can learn more about the Python __getitem__ in the resource below:

https://docs.python.org/3/reference/datamodel.html#object.__getitem__

Conclusion

This article discussed the Python unsubscriptable error, what causes it, and how to fix it.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list