Stay Hungry,Stay Foolish!

Subscriptable Objects in Python

Python TypeError: ‘NoneType’ object is not subscriptable

 对None进行脚本标注, 来获取值得写法,产生报错。

subscript 来源 于数学中, 对集合对象 进行索引子元素的写法 a1 其中的1是对a的脚注。

 

https://itsmycode.com/python-typeerror-nonetype-object-is-not-subscriptable/

If you subscript any object with None value, Python will raise TypeError: ‘NoneType’ object is not subscriptable exception. The term subscript means retrieving the values using indexing.

In this tutorial, we will learn what is NoneType object is not subscriptable error means and how to resolve this TypeError in your program with examples.

 

https://stackoverflow.com/questions/216972/what-does-it-mean-if-a-python-object-is-subscriptable-or-not?rq=1

The [...] indexing syntax is called a subscript, because it's equivalent to mathematical notation that uses actual subscripts; e.g. a[1] is Python for what mathematicians would write as a₁. So "subscriptable" means "able to be subscripted". Which, in Python terms, means it has to implement __getitem__(), since a[1] is just syntactic sugar for a.__getitem__(1). Apr 2 '20 at 14:15

 

Subscriptable Objects

https://www.pythonpool.com/method-object-is-not-subscriptable/#:~:text=What%20are%20Subscriptable%20Objects%20in%20Python%3F%20Subscriptable%20objects,object%20to%20make%20them%20compatible%20for%20accessing%20elements.

可脚注的对象, 其实现的 魔法函数 __getitem__

例如 list dict tuple

What are Subscriptable Objects in Python?

Subscriptable objects are the objects in which you can use the [item] method using square brackets. For example, to index a list, you can use the list[1] way.

Inside the class, the __getitem__ method is used to overload the object to make them compatible for accessing elements. Currently, this method is already implemented in lists, dictionaries, and tuples. Most importantly, every time this method returns the respective elements from the list.

Now, the problem arises when objects with the __getitem__ method are not overloaded and you try to subscript the object. In such cases, the ‘method’ object is not subscriptable error arises.

 

__getitem__

https://blog.finxter.com/python-typeerror-nonetype-object-is-not-subscriptable/

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.

 

posted @   lightsong  阅读(450)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2021-01-21 Sample pipeline for text feature extraction and evaluation of sklearn
2021-01-21 Clustering text documents using k-means of sklearn
2021-01-21 Feature extraction of sklearn
千山鸟飞绝,万径人踪灭
点击右上角即可分享
微信分享提示