>> 不支持的操作数类型:'Queue' 和 'int' 在实施“__rrshift__”后
unsupported operand type(s) for >>: 'Queue' and 'int' after implementing '__rrshift__'
class Queue:
queue_list: list
def __init__(self, *args: int) -> None:
self.queue_list = list(args)
...
def __rrshift__(self, i: int) -> Queue:
result: Queue = self.copy()
try:
result.queue_list = self.queue_list[i:]
except IndexError:
pass
return result
q3 = Queue(2, 3, 4, 5)
q4 = Queue(1, 2)
q4 += q3 >> 4
print(q4)
File "c:\...\test.py", line 19, in <module>
q4 += q3 >> 4
TypeError: unsupported operand type(s) for >>: 'Queue' and 'int'
>>
必须 return 新的 Queue()
和 self.queue_list[i:]
如果可能的话清除 Queue()
.
错误是什么?
__rrshift__
实现 Queue
实例位于 >>
运算符的 right-hand 侧的操作。
为了能够使用 q3 >> 4
,Queue
在 left-hand 一侧,您需要实施 __rshift__
.
语法 a >> b
等同于 a.__rshift__(b)
。如果没有为对象 a
定义此方法,它会查找 b.__rrshift__(a)
,即附加的 r
前缀代表 reverse 运算符。
所以在你的情况下 q3 >> 4
需要 q3.__rshift__(4)
.
如果你已经实现了 __rrshift__
你可以做 4 >> q3
,因为 int.__rshift__
没有用 Queue
作为参数定义(更准确地说它引发了一个NotImplemented
例外),所以它然后使用 q3.__rrshift__(4)
.
class Queue:
queue_list: list
def __init__(self, *args: int) -> None:
self.queue_list = list(args)
...
def __rrshift__(self, i: int) -> Queue:
result: Queue = self.copy()
try:
result.queue_list = self.queue_list[i:]
except IndexError:
pass
return result
q3 = Queue(2, 3, 4, 5)
q4 = Queue(1, 2)
q4 += q3 >> 4
print(q4)
File "c:\...\test.py", line 19, in <module>
q4 += q3 >> 4
TypeError: unsupported operand type(s) for >>: 'Queue' and 'int'
>>
必须 return 新的 Queue()
和 self.queue_list[i:]
如果可能的话清除 Queue()
.
错误是什么?
__rrshift__
实现 Queue
实例位于 >>
运算符的 right-hand 侧的操作。
为了能够使用 q3 >> 4
,Queue
在 left-hand 一侧,您需要实施 __rshift__
.
语法 a >> b
等同于 a.__rshift__(b)
。如果没有为对象 a
定义此方法,它会查找 b.__rrshift__(a)
,即附加的 r
前缀代表 reverse 运算符。
所以在你的情况下 q3 >> 4
需要 q3.__rshift__(4)
.
如果你已经实现了 __rrshift__
你可以做 4 >> q3
,因为 int.__rshift__
没有用 Queue
作为参数定义(更准确地说它引发了一个NotImplemented
例外),所以它然后使用 q3.__rrshift__(4)
.