- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
假设下面的类:
class Class(object):
@classmethod
def getitem(*args):
print 'getitem %s' % (args,)
@classmethod
def __getitem__(*args):
print '__getitem__ %s' % (args,)
getitem 方法的行为符合预期:它接收 Class
作为第一个 arg,但是 __getitem__
接收 type
作为第一个 arg:
calling Class.getitem(test)
getitem (<class '__main__.Class'>, 'test')
calling obj.getitem(test)
getitem (<class '__main__.Class'>, 'test')
calling Class[test]
'type' object has no attribute '__getitem__'
calling obj[test]
__getitem__ (<class '__main__.Class'>, 'test')
__getitem__
背后的魔法是什么?
最佳答案
特殊方法是在类中查找的,而不是在实例中查找的——这与首先在实例中查找的常规方法不同。参见 Special method lookup在 Python 数据模型文档中。
将 Class
视为 type
的实例,这意味着当您这样做时
Class.getitem(test)
它首先准确查找您告诉它的内容:Class
自己的属性中称为 getitem
的方法。但是,当你使用
Class[test]
它跳过这个,直接进入type
(是Class
的类,或者它的元类),然后调用type.__getitem__(Class,测试)
。所以,发生的事情不是 __getitem__
获取 type
作为它的第一个参数(它仍然会获取 Class
,就像你显式 Class.__getitem__(test)
), 是Python在这种情况下寻找的__getitem__
不存在。要使其存在,您需要为 Class
定义自己的元类,将其定义为实例方法,而不是在 Class
上将其定义为类方法。
关于python - 为什么 __getitem__ 不能是类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12447036/
当 __getitem__ 映射到内部序列类型时,以下哪一项是推荐的执行方式? class A: def __init__(self, ...): ... se
这是我在leetcode中遇到的问题。您将看到两个非空链接表,表示两个非负整数。数字以相反的顺序存储,并且它们的每个节点都包含一个数字。将这两个数字相加,然后以链表的形式返回总和。。你可以假设这两个数
我正在通过继承 torch.utils.data.Dataset 创建一个数据集类并遇到了以下问题。 与返回固定类型值的先前函数不同,__getitem__没有。例如, class MyExample
我有一个自定义类Field的对象,它本质上包裹着一个numpy.ndarray对象。该对象由两个输入定义:一个值数组 (values) 和一个切片对象 (segment),该对象定义这些值应放置在某个
我有一个简单的问题,这行代码 row[time] == numbers[num_time]: 给我错误: int has no attribute __getitem__ 经过一些研究,我发现当您尝试
我是 python 的初学者,正在尝试通过测试 test_pic 和包含图像的数据库来查找图像之间的相似性。我已经从目录传递了图像并使用 SIFT 功能对其进行了比较 from PIL import
这是 Python 在切片中传递负数的行为示例: >>> class test: ... def __getitem__(self, sl): ... print sl.st
我正在制作一个树类,我想要 __getitem__方法来获取元组参数,所以我这样使用它: t[1, 2, 3] 但是,当我想获取根值时,我需要给它一个空元组,当我这样做时 t[] 我收到语法错误: >
我正在创建一个表示列表列表的类。 __getitem__ 让我头疼。在我将切片作为参数引入之前,一切都进行得很顺利。 演示代码 # Python version 2.7.5 class NestedL
序列(例如列表)的方法 __getitem__() 可以返回单个项目或项目序列。例如,给定下面的函数装饰: def __getitem__(self, index) -> Union[Product,
我正在编写基本的 PSO(粒子群优化),并且一直收到此错误,即粒子实例没有属性 __getitem__。我认为一切都很好,但 article 类似乎有一些错误。看一下 article 类。 from
我无法理解我收到此错误 -> 实例方法没有属性 getitem。我只是想抓取这个网站以提取部门名称。 import scrapy from scrapy.contrib.spiders import
你好,StackOverflowers, 我正在实现一个二叉搜索树,其接口(interface)与 Python 中的 dict 几乎相同(在有人问之前,我这样做是为了好玩,没有生产代码)。 为了从我
这个问题在这里已经有了答案: How to inherit and extend a list object in Python? (4 个答案) 关闭 5 个月前。 我正在尝试定义一个默认样式列表
我为使用列表列表的 connect 4 游戏创建了一个 Board 类。我想让对象支持对面板的行和列进行索引: class Test: def __init__(self, cols, row
有没有办法以支持整数和切片索引的方式实现 __getitem__ 而无需手动检查参数类型? 我看到了很多这种形式的例子,但我觉得它很老套。 def __getitem__(self,key): i
class Custom(type): @classmethod def __getitem__(cls, item): raise NotImplementedErr
我有这个代码: class A: def __init__(self): def method(self, item): print self, ":
python 中是否有一些内部的东西来处理传递给 __getitem_ 的参数 _ 不同,并自动将 start:stop:step 构造转换为切片? 下面是我的意思的演示 class ExampleC
我有两个这样的模型: class School(models.Model): name = models.CharField(max_length = 50) def __unicod
我是一名优秀的程序员,十分优秀!