如果一个对象是可订阅的,这意味着什么?

    技术2025-08-30  10

    本文翻译自:what does it mean if an object is subscriptable or not?

    哪些类型的对象属于“可订阅”域?


    #1楼

    参考:https://stackoom.com/question/uRY/如果一个对象是可订阅的-这意味着什么


    #2楼

    It basically means that the object implements the __getitem__() method. 它基本上意味着该对象实现了__getitem__()方法。 In other words, it describes objects that are "containers", meaning they contain other objects. 换句话说,它描述了作为“容器”的对象,这意味着它们包含其他对象。 This includes lists, tuples, and dictionaries. 这包括列表,元组和词典。


    #3楼

    A scriptable object is an object that records the operations done to it and it can store them as a "script" which can be replayed. 可编写脚本的对象是一个记录对其执行的操作的对象,它可以将它们存储为可以重放的“脚本”。

    For example, see: Application Scripting Framework 例如,请参阅: Application Scripting Framework

    Now, if Alistair didn't know what he asked and really meant "subscriptable" objects (as edited by others), then (as mipadi also answered) this is the correct one: 现在,如果Alistair不知道他问的是什么并且真正意味着“可订阅的”对象(由其他人编辑),那么(正如mipadi也回答)这是正确的:

    A subscriptable object is any object that implements the __getitem__ special method (think lists, dictionaries). 可订阅对象是实现__getitem__特殊方法的任何对象(思考列表,字典)。


    #4楼

    Off the top of my head, the following are the only built-ins that are subscriptable: 在我的脑海中,以下是唯一可订阅的内置插件:

    string: "foobar"[3] == "b" tuple: (1,2,3,4)[3] == 4 list: [1,2,3,4][3] == 4 dict: {"a":1, "b":2, "c":3}["c"] == 3

    But mipadi's answer is correct - any class that implements __getitem__ is subscriptable 但是mipadi的答案是正确的 - 任何实现__getitem__类都是可订阅的


    #5楼

    I had this same issue. 我有同样的问题。 I was doing 我在做

    arr = [] arr.append["HI"]

    So using [ was causing error. 因此使用[导致错误。 It should be arr.append("HI") 它应该是arr.append("HI")


    #6楼

    The meaning of subscript in computing is: "a symbol (notionally written as a subscript but in practice usually not) used in a program, alone or with others, to specify one of the elements of an array." 计算中下标的含义是:“一个符号(在概念上写成,但在实践中通常不是),在程序中单独使用或与其他程序一起使用,以指定数组的一个元素。”

    Now, in the simple example given by @user2194711 we can see that the appending element is not able to be a part of the list because of two reasons:- 现在,在@ user2194711给出的简单示例中,我们可以看到附加元素不能成为列表的一部分,原因有两个: -

    1) We are not really calling the method append; 1)我们并没有真正调用方法追加; because it needs () to call it. 因为它需要()来调用它。

    2) The error is indicating that the function or method is not subscriptable; 2)错误表明该函数或方法不可订阅; means they are not indexable like a list or sequence. 意味着它们不像列表或序列那样可索引。

    Now see this:- 现在看到这个: -

    >>> var = "myString" >>> def foo(): return 0 ... >>> var[3] 't' >>> foo[3] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'function' object is not subscriptable

    That means there are no subscripts or say elements in function like they occur in sequences; 这意味着没有下标或说出function元素,就像它们出现在序列中一样; and we cannot access them like we do, with the help of [] . 在[]的帮助下,我们无法像我们一样访问它们。

    Also; 也; as mipadi said in his answer; 正如米帕迪在回答中说的那样; It basically means that the object implements the __getitem__() method. 它基本上意味着该对象实现了__getitem__()方法。 (if it is subscriptable). (如果是可订阅的)。 Thus the error produced: 因此产生的错误:

    arr.append["HI"]

    TypeError: 'builtin_function_or_method' object is not subscriptable TypeError:'builtin_function_or_method'对象不可订阅

    Processed: 0.023, SQL: 9