为什么不给__getitem__分配一个类呢?

时间:2019-02-13 13:56:40

标签: python python-3.x class python-3.6

这里是一个list子类,它将其项目委托给itertools.compress

from itertools import compress

class WeirdList(list):
    def __getitem__(self, item):
        return compress(self, item)

l = WeirdList([1, 2, 3, 4])
print(*l[0, 1, 0, 1]) # 2 4

尽管我觉得我可以直接将compress分配给__getitem__,但上述方法效果很好。

class WeirdList(list):
    __getitem__ = compress

l = WeirdList([1, 2, 3, 4])
print(*l[0, 1, 0, 1])

这引发了以下内容:

Traceback (most recent call last):
  File "...", line 7, in <module> print(*l[0, 1, 0, 1])
TypeError: Required argument 'selectors' (pos 2) not found

我相信这会中断,因为compress是一个类而不是一个函数,但是消息显示TypeError是从调用compress引发的。

__getitem__协议在什么时候使用单个参数调用compress

1 个答案:

答案 0 :(得分:3)

函数可以用作方法,因为它具有__get__属性。 class compress没有__get__属性:

>>> compress.__get__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module> 
AttributeError: type object 'itertools.compress' has no attribute '__get__'  

所以不能是方法。

使用__get__方法调用属性时,将调用__get__方法并返回其返回值,而不是属性本身的值。即l[0] == l.__getitem__(0) == l.__getitem__.__get__(l, type(l))(0),其中__get__的返回值是一个已经将l传递给函数的对象。

(如果您曾经想知道classmethodstaticmethod装饰器的作用,它们将使用不同的__get__方法返回对象。)

相关问题