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'
>> must return new Queue() with self.queue_list[i:] if it possible else clear Queue().
What is the error?
CodePudding user response:
__rrshift__ implements the operation where a Queue instance is on the right-hand side of the >> operator.
In order to be able to use q3 >> 4, where a Queue is on the left-hand side you need to implement __rshift__.
The syntax a >> b is equivalent to a.__rshift__(b). If this method isn't defined for the object a it then looks up b.__rrshift__(a), i.e. the additional r prefix stands for reverse operator.
So in your case q3 >> 4 needs q3.__rshift__(4).
If you have implemented __rrshift__ you can do 4 >> q3, because int.__rshift__ isn't defined with a Queue as argument (to be more precise it raises a NotImplemented exception), so it then uses q3.__rrshift__(4).
