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

  • 相关阅读:
    TCP/IP协议栈概述及各层包头分析
    Maven:Non-resolvable parent POM: Failure to find错误
    mysql alter 用法,修改表,字段等信息
    PowerDesigner16 设置导出sql文件的编码
    linux iptables开放/关闭端口命令
    Enterprise Architect 13 : 需求建模 自动命名并计数
    Enterprise Architect 13 : 将绘制的图形导出成图片 或者 拷贝到剪贴板中
    Enterprise Architect 13 : 设置默认代码环境
    使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,报异常的解决方法
    PowerDesigner16 修改表或表的字段Name的时候不让Code不自动跟着变
  • 原文地址:https://www.cnblogs.com/lightsong/p/15832140.html
Copyright © 2020-2023  润新知