重载 __getitem__ 接受另一个参数
overloaded __getitem__ accepting another argument
我想让一个 class 的 __getitem__
成为它与另一个列表中同一索引处的项目的总和。
>>> class Test(list):
def __init__(self):
self.extend([1, 2, 3])
def __getitem__(self, index, other):
return self[index] + other[index]
>>> t = Test()
>>> t2 = [4, 5, 6]
但是,我的两次尝试都出现了错误:
>>> t[5, t2]
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
t[5, t2]
TypeError: __getitem__() missing 1 required positional argument: 'other'
>>> t.__getitem__(5, t2)
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
t.__getitem__(5, t2)
File "<pyshell#17>", line 5, in __getitem__
return self[index] + other[index]
TypeError: __getitem__() missing 1 required positional argument: 'other'
是否可以给 __getitem__
多个参数?如果是这样,如何?如果没有,有没有办法模拟它?
这是可能的,因为你在 __getitem__
中得到一个 "indices" 的元组,而不是多个参数:
class Test(list):
def __init__(self):
self.extend([1, 2, 3])
def __getitem__(self, value):
# this makes sure that a sequence with exactly 2 elements is passed in: (thanks @ShadowRanger)
index, other = value
return super().__getitem__(index) + other[index]
>>> t = Test()
>>> t2 = [4, 5, 6]
>>> t[2, t2]
9 # 3 + 6
>>> t[1, t2]
7 # 2 + 5
>>> t[0, t2]
5 # 1 + 4
但是有一些注意事项:
- 您需要显式调用
super().__getitem__
这样您就不会以递归结束。
- 如果要允许正常切片,您必须注意传递给
__getitem__
的参数数量。
- 这种行为会让用户感到困惑,所以我实际上不建议使用它!
Is it possible to give getitem multiple arguments? If so, how? If not, is there a way to emulate it?
为什么要效仿这个?您错过了 "magic methods" 的要点。 Python 为您提供这些方法以提供一种运算符重载形式。这是 Python 文档部分 3.3 的摘录。特殊方法名称 ,描述了 "magic methods" 的用途:
A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python’s approach to operator overloading, allowing classes to define their own behavior with respect to language operators.
(强调我的)
此外,这会使您的 class 对于期望 __getitem__
到 return 单个元素的代码读者而言含糊不清。
由于@MSeifert 已经为您提供了实现所需行为的方法,因此我不会 post 我的解决方案。但我强烈建议您创建自己的自定义方法。如果您仍然选择更改 __getitem__
方法的功能,我强烈建议您至少记录这些更改并明确说明 __getiem__
的实现与正常实现的行为不同.
我想让一个 class 的 __getitem__
成为它与另一个列表中同一索引处的项目的总和。
>>> class Test(list):
def __init__(self):
self.extend([1, 2, 3])
def __getitem__(self, index, other):
return self[index] + other[index]
>>> t = Test()
>>> t2 = [4, 5, 6]
但是,我的两次尝试都出现了错误:
>>> t[5, t2]
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
t[5, t2]
TypeError: __getitem__() missing 1 required positional argument: 'other'
>>> t.__getitem__(5, t2)
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
t.__getitem__(5, t2)
File "<pyshell#17>", line 5, in __getitem__
return self[index] + other[index]
TypeError: __getitem__() missing 1 required positional argument: 'other'
是否可以给 __getitem__
多个参数?如果是这样,如何?如果没有,有没有办法模拟它?
这是可能的,因为你在 __getitem__
中得到一个 "indices" 的元组,而不是多个参数:
class Test(list):
def __init__(self):
self.extend([1, 2, 3])
def __getitem__(self, value):
# this makes sure that a sequence with exactly 2 elements is passed in: (thanks @ShadowRanger)
index, other = value
return super().__getitem__(index) + other[index]
>>> t = Test()
>>> t2 = [4, 5, 6]
>>> t[2, t2]
9 # 3 + 6
>>> t[1, t2]
7 # 2 + 5
>>> t[0, t2]
5 # 1 + 4
但是有一些注意事项:
- 您需要显式调用
super().__getitem__
这样您就不会以递归结束。 - 如果要允许正常切片,您必须注意传递给
__getitem__
的参数数量。 - 这种行为会让用户感到困惑,所以我实际上不建议使用它!
Is it possible to give getitem multiple arguments? If so, how? If not, is there a way to emulate it?
为什么要效仿这个?您错过了 "magic methods" 的要点。 Python 为您提供这些方法以提供一种运算符重载形式。这是 Python 文档部分 3.3 的摘录。特殊方法名称 ,描述了 "magic methods" 的用途:
A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python’s approach to operator overloading, allowing classes to define their own behavior with respect to language operators.
(强调我的)
此外,这会使您的 class 对于期望 __getitem__
到 return 单个元素的代码读者而言含糊不清。
由于@MSeifert 已经为您提供了实现所需行为的方法,因此我不会 post 我的解决方案。但我强烈建议您创建自己的自定义方法。如果您仍然选择更改 __getitem__
方法的功能,我强烈建议您至少记录这些更改并明确说明 __getiem__
的实现与正常实现的行为不同.