[Tut] Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug) - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug) (/thread-98234.html) |
[Tut] Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug) - xSicKxBot - 11-12-2020 Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug) <div><p>Do you encounter this stupid error?</p> <div class="wp-block-image"> <figure class="aligncenter size-large"><img loading="lazy" width="762" height="140" src="https://blog.finxter.com/wp-content/uploads/2020/11/image-20.png" alt=" TypeError: 'NoneType' object is not subscriptable" class="wp-image-16736" srcset="https://blog.finxter.com/wp-content/uploads/2020/11/image-20.png 762w, https://blog.finxter.com/wp-content/uploads/2020/11/image-20-300x55.png 300w, https://blog.finxter.com/wp-content/uploads/2020/11/image-20-150x28.png 150w" sizes="(max-width: 762px) 100vw, 762px" /></figure> </div> <p>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!</p> <p class="has-pale-cyan-blue-background-color has-background">Python throws the <code>TypeError object is not subscriptable</code> 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 <code>__getitem__()</code> method. You can fix it by removing the indexing call or defining the <code>__getitem__</code> method.</p> <p>The following code snippet shows the minimal example that leads to the error:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">variable = None print(variable[0]) # TypeError: 'NoneType' object is not subscriptable</pre> <p>You set the <code>variable</code> to the value <code>None</code>. The value <code data-enlighter-language="generic" class="EnlighterJSRAW">None</code> is not a container object, it doesn’t <em>contain </em>other objects. So, the code really doesn’t make any sense—which result do you expect from the indexing operation?</p> <p><em><strong>Exercise</strong>: Before I show you how to fix it, try to resolve the error yourself in the following interactive shell:</em></p> <p> <iframe src="https://trinket.io/embed/python/e55b915062" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p> <p>If you struggle with indexing in Python, have a look at the following articles on the Finxter blog—especially the third!</p> <p><strong>Related Articles:</strong></p> <ul> <li><a href="https://blog.finxter.com/daily-python-puzzle-list-indexing/" title="List Indexing" target="_blank" rel="noreferrer noopener">Indexing in Python</a></li> <li><a href="https://blog.finxter.com/introduction-to-slicing-in-python/" title="Introduction to Slicing in Python" target="_blank" rel="noreferrer noopener">Slicing in Python</a></li> <li>Highly Recommended: <a href="https://blog.finxter.com/accessing-the-index-of-iterables-in-python/" title="Accessing The Index Of Iterables In Python" target="_blank" rel="noreferrer noopener">Accessing the Index of Iterables in Python</a></li> </ul> <p>Note that a similar problem arises if you set the variable to the integer value <code>42</code> instead of the <code>None</code> value. The only difference is that the error message now is <code>"TypeError: 'int' object is not subscriptable"</code>.</p> <div class="wp-block-image"> <figure class="aligncenter size-large"><img loading="lazy" width="805" height="204" src="https://blog.finxter.com/wp-content/uploads/2020/11/image-19.png" alt="TypeError: 'int' object is not subscriptable" class="wp-image-16735" srcset="https://blog.finxter.com/wp-content/uploads/2020/11/image-19.png 805w, https://blog.finxter.com/wp-content/uploads/2020/11/image-19-300x76.png 300w, https://blog.finxter.com/wp-content/uploads/2020/11/image-19-768x195.png 768w, https://blog.finxter.com/wp-content/uploads/2020/11/image-19-150x38.png 150w" sizes="(max-width: 805px) 100vw, 805px" /></figure> </div> <p>You can fix the<em> non-subscriptable TypeError </em>by wrapping the non-indexable values into a container data type such as a <a href="https://blog.finxter.com/python-lists/" title="The Ultimate Guide to Python Lists" target="_blank" rel="noreferrer noopener">list </a>in Python:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">variable = [None] print(variable[0]) # None</pre> <p>The output now is the value <code>None</code> and the script doesn’t throw an error anymore.</p> <p>An alternative is to define the <code>__getitem__</code> method in your code:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class X: def __getitem__(self, i): return f"Value {i}" variable = X() print(variable[0]) # Value 0 </pre> <p>You overwrite the <code>__getitem__</code> method that takes one (index) argument <code>i</code> (in addition to the obligatory <code><a href="https://blog.finxter.com/self-in-python/" target="_blank" rel="noreferrer noopener" title="Python Self — An Interactive Guide with Video">self</a></code> argument) and returns the <code>i</code>-th value of the “container”. In our case, we just return a string <code>"Value 0"</code> for the element <code>variable[0]</code> and <code>"Value 10"</code> for the element <code>variable[10]</code>. It doesn’t make a lot of sense here but is the minimal example that shows how it works.</p> <p>I hope you’d be able to fix the bug in your code! Before you go, check out our <strong><em>free Python cheat sheets</em></strong> that’ll teach you the basics in Python in minimal time:</p> <p>The post <a href="https://blog.finxter.com/python-typeerror-nonetype-object-is-not-subscriptable/" target="_blank" rel="noopener noreferrer">Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug)</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p> </div> https://www.sickgaming.net/blog/2020/11/12/python-typeerror-object-is-not-subscriptable-how-to-fix-this-stupid-bug/ |