• 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.

  • 相关阅读:
    Locust性能测试-环境准备与基本使用 转自:悠悠
    Locust性能测试_先登录场景案例
    python_协程
    python_进程池以及线程池
    python_并发与通信
    python_进程与线程的补充
    python_并行与并发、多线程
    Hash算法:双重散列
    AVL树的插入和删除
    查找最小生成树:普里姆算法算法(Prim)算法
  • 原文地址:https://www.cnblogs.com/lightsong/p/15832140.html
Copyright © 2020-2023  润新知