在Python中,如果一个对象是可定义的,它意味着什么?

时间:2022-06-08 16:57:40

Which types of objects fall into the domain of "subscriptable"?

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

5 个解决方案

#1


188  

It basically means that the object implements the __getitem__() method. In other words, it describes objects that are "containers", meaning they contain other objects. This includes lists, tuples, and dictionaries.

它基本上意味着对象实现了__getitem__()方法。换句话说,它描述的对象是“容器”,意思是它们包含其他对象。这包括列表、元组和字典。

#2


40  

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__的类都是可订阅的

#3


12  

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

例如,请参见:应用程序脚本框架

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:

现在,如果阿利斯泰尔不知道他问的是什么,而真正的意思是“可订阅的”对象(由其他人编辑),那么(正如米帕迪也回答的那样)这才是正确的:

A subscriptable object is any object that implements the __getitem__ special method (think lists, dictionaries).

subscriptable对象是任何实现__getitem__特殊方法(think list, dictionary)的对象。

#4


2  

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(“嗨”)

#5


0  

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 it is itself a list. Thus the error produced:

现在,在@user2194711给出的简单示例中,我们可以看到追加元素不能成为列表的一部分,因为它本身就是一个列表。因此产生的错误:

arr.append["HI"]

TypeError: 'builtin_function_or_method' object is not subscriptable

TypeError:“builtin_function_or_method”对象不是可下标的。

#1


188  

It basically means that the object implements the __getitem__() method. In other words, it describes objects that are "containers", meaning they contain other objects. This includes lists, tuples, and dictionaries.

它基本上意味着对象实现了__getitem__()方法。换句话说,它描述的对象是“容器”,意思是它们包含其他对象。这包括列表、元组和字典。

#2


40  

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__的类都是可订阅的

#3


12  

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

例如,请参见:应用程序脚本框架

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:

现在,如果阿利斯泰尔不知道他问的是什么,而真正的意思是“可订阅的”对象(由其他人编辑),那么(正如米帕迪也回答的那样)这才是正确的:

A subscriptable object is any object that implements the __getitem__ special method (think lists, dictionaries).

subscriptable对象是任何实现__getitem__特殊方法(think list, dictionary)的对象。

#4


2  

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(“嗨”)

#5


0  

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 it is itself a list. Thus the error produced:

现在,在@user2194711给出的简单示例中,我们可以看到追加元素不能成为列表的一部分,因为它本身就是一个列表。因此产生的错误:

arr.append["HI"]

TypeError: 'builtin_function_or_method' object is not subscriptable

TypeError:“builtin_function_or_method”对象不是可下标的。