Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重载\uuuu getitem\uuuu接受另一个参数_Python_Python 3.x_Overloading - Fatal编程技术网

Python 重载\uuuu getitem\uuuu接受另一个参数

Python 重载\uuuu getitem\uuuu接受另一个参数,python,python-3.x,overloading,Python,Python 3.x,Overloading,我想让一个类的\uuu getitem\uuu成为它和另一个列表的同一索引中的项的总和 >>> class Test(list): def __init__(self): self.extend([1, 2, 3]) def __getitem__(self, index, other): return self[index] + other[index] >>> t = Test() >>>

我想让一个类的
\uuu getitem\uuu
成为它和另一个列表的同一索引中的项的总和

>>> 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'
>t[5,t2]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
t[5,t2]
TypeError:\uuuuGetItem\uuuuuu()缺少1个必需的位置参数:“其他”
>>>t.uu获取项目(5,t2)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
t、 获取项目(5,t2)
文件“”,第5行,在\uu getitem中__
返回自[索引]+其他[索引]
TypeError:\uuuuGetItem\uuuuuu()缺少1个必需的位置参数:“其他”

是否可以为
\uuu getitem\uuu
提供多个参数?如果是,怎么做?如果没有,是否有方法模拟它?

这是可能的,因为您在
\uu getitem\uuuuuuuuuuuuuu>中得到了一个“索引”元组,而不是多个参数:

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()。\uuuu getitem\uuuu
    ,这样您就不会陷入递归
  • 如果要允许正常切片,则必须注意传递给
    \uuu getitem\uuu
    的参数数量
  • 这种行为会让用户感到困惑,所以我不推荐使用它
是否可以为getitem提供多个参数?如果是,怎么做?如果没有,有没有办法效仿

为什么要效仿这一点?你错过了“神奇方法”的要点。Python为您提供了这些方法来提供一种形式的运算符重载。下面是Python文档第3.3节的摘录。特殊方法名称,用于描述“神奇方法”的用途:

类可以通过定义具有特殊名称的方法来实现由特殊语法(如算术运算或订阅和切片)调用的某些操作这是Python的操作符重载方法,允许类定义自己关于语言操作符的行为

(强调矿山)

此外,这使得您的类对于希望
\uuu getitem\uuu
返回单个元素的代码读者来说是不明确的


由于@MSeifert已经为您提供了一种实现所需行为的方法,因此我不会发布我的解决方案。但我强烈建议您创建自己的自定义方法。如果您仍然选择更改
\uu getitem\uuuuuuu
方法的功能,我强烈建议您至少记录这些更改,并明确说明
\uuuu getiem\uuuuuu
的实现与正常实现具有不同的行为。

为了确保不会获得比预期更多的参数,我会跳过显式索引,只使用序列解包:
index,other=value
。它的速度也更快(Python针对廉价的
tuple
packing和unpacking进行了优化,而对于索引的优化则更少)。我同意这是。。。古怪的至少可以这么说,我建议不要这样做,因为可能会引起混淆。我的灵感来自于回答你的第一段:
matrix[I,j]
matrix[x,y,z]
是模仿这一点的好理由。它读起来比矩阵要漂亮得多。获取项目(x,y,z)
@Guimoute我完全同意哈哈。我差不多五年前写过这个问题,那时我还是一个相当新的程序员,几乎没有使用过像
numpy
这样的库的经验,这些库使用多个参数来
\uuuuu getitem\uuuu
。长话短说,这个答案很好地显示了我的天真!现在,我仍然不推荐OP正在做什么,因为他正在传递一个列表!