Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug)

#1
Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug)

Do you encounter this stupid error?

 TypeError: 'NoneType' object is not subscriptablehttps://blog.finxter.com/wp-content/uplo...300x55.png 300w, https://blog.finxter.com/wp-content/uplo...150x28.png 150w" sizes="(max-width: 762px) 100vw, 762px" />

You’re not alone—thousands of coders like you generate this error in thousands of projects every single month. This short tutorial will show you exactly why this error occurs, how to fix it, and how to never make the same mistake again. So, let’s get started!

Python throws the TypeError object is not subscriptable if you use indexing with the square bracket notation on an object that is not indexable. This is the case if the object doesn’t define the __getitem__() method. You can fix it by removing the indexing call or defining the __getitem__ method.

The following code snippet shows the minimal example that leads to the error:

variable = None
print(variable[0])
# TypeError: 'NoneType' object is not subscriptable

You set the variable to the value None. The value None is not a container object, it doesn’t contain other objects. So, the code really doesn’t make any sense—which result do you expect from the indexing operation?

Exercise: Before I show you how to fix it, try to resolve the error yourself in the following interactive shell:

If you struggle with indexing in Python, have a look at the following articles on the Finxter blog—especially the third!

Related Articles:

Note that a similar problem arises if you set the variable to the integer value 42 instead of the None value. The only difference is that the error message now is "TypeError: 'int' object is not subscriptable".

TypeError: 'int' object is not subscriptablehttps://blog.finxter.com/wp-content/uplo...300x76.png 300w, https://blog.finxter.com/wp-content/uplo...68x195.png 768w, https://blog.finxter.com/wp-content/uplo...150x38.png 150w" sizes="(max-width: 805px) 100vw, 805px" />

You can fix the non-subscriptable TypeError by wrapping the non-indexable values into a container data type such as a list in Python:

variable = [None]
print(variable[0])
# None

The output now is the value None and the script doesn’t throw an error anymore.

An alternative is to define the __getitem__ method in your code:

class X: def __getitem__(self, i): return f"Value {i}" variable = X()
print(variable[0])
# Value 0

You overwrite the __getitem__ method that takes one (index) argument i (in addition to the obligatory self argument) and returns the i-th value of the “container”. In our case, we just return a string "Value 0" for the element variable[0] and "Value 10" for the element variable[10]. It doesn’t make a lot of sense here but is the minimal example that shows how it works.

I hope you’d be able to fix the bug in your code! Before you go, check out our free Python cheat sheets that’ll teach you the basics in Python in minimal time:

The post Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug) first appeared on Finxter.



https://www.sickgaming.net/blog/2020/11/...tupid-bug/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Show Don’t Tell: Commenting Python Best Practices xSicKxBot 0 4 11-27-2020, 03:41 AM
Last Post: xSicKxBot
  [Tut] How to Catch and Print Exception Messages in Python xSicKxBot 0 6 11-23-2020, 11:45 PM
Last Post: xSicKxBot
  [Tut] Python next() xSicKxBot 0 8 11-22-2020, 08:17 AM
Last Post: xSicKxBot
  [Tut] Python Math Domain Error (How to Fix This Stupid Bug) xSicKxBot 0 11 11-21-2020, 08:01 AM
Last Post: xSicKxBot
  [Tut] Python Int to String with Leading Zeros xSicKxBot 0 18 11-17-2020, 03:22 AM
Last Post: xSicKxBot
  [Tut] Python Function Call Inside List Comprehension xSicKxBot 0 25 11-16-2020, 02:42 AM
Last Post: xSicKxBot
  [Tut] How to Generate Text Automatically With Python? A Guide to the DeepAI API xSicKxBot 0 34 11-10-2020, 06:23 AM
Last Post: xSicKxBot
  [Tut] How to Get MD5 of a String? A Python One-Liner xSicKxBot 0 31 11-07-2020, 03:58 AM
Last Post: xSicKxBot
  [Tut] Overriding in Python xSicKxBot 0 39 10-29-2020, 04:49 AM
Last Post: xSicKxBot
  [Tut] How to Install PIL/Pillow in Python? A Helpful Illustrated Guide xSicKxBot 0 27 10-27-2020, 12:00 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
Hands-On With Sound Particles
Last Post: xSicKxBot
Yesterday 11:16 PM
» Replies: 0
» Views: 3
AppleInsider - Better than Black Friday ...
Last Post: xSicKxBot
Yesterday 11:15 PM
» Replies: 0
» Views: 2
News - AOC Is Streaming Among Us Right N...
Last Post: xSicKxBot
Yesterday 11:13 PM
» Replies: 0
» Views: 2
(Indie Deal) FREE Clockwise, 80% OFF XCO...
Last Post: xSicKxBot
Yesterday 05:46 PM
» Replies: 0
» Views: 2
Xbox Wire - Bring on the Holiday Cheer w...
Last Post: xSicKxBot
Yesterday 05:19 PM
» Replies: 0
» Views: 2
News - MechWarrior developer Piranha Gam...
Last Post: xSicKxBot
Yesterday 05:18 PM
» Replies: 0
» Views: 3
News - The Crown Prince Of Saudi Arabia ...
Last Post: xSicKxBot
Yesterday 05:10 PM
» Replies: 0
» Views: 2
News - Nintendo Switch Looks Poised To S...
Last Post: xSicKxBot
Yesterday 05:10 PM
» Replies: 0
» Views: 2
News - Amazon Black Friday Anime Sale Ha...
Last Post: xSicKxBot
Yesterday 05:10 PM
» Replies: 0
» Views: 3
[1.06] BO4 Zombie Trainer (Call of duty ...
Last Post: imsirgoku
Yesterday 10:23 AM
» Replies: 30
» Views: 1166

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020