You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
the attached code in t.py fails to run:
class C (object):
def __ror__ (self, other):
return 42
print C() | C()
$ python t.py
Traceback (most recent call last):
File "t.py", line 5, in ?print C() | C()
TypeError: unsupported operand type(s) for |: 'C' and 'C'
If I use old style classes (ie. "class C:" instead of "class
C(object):"), the code runs fine. I suspect that the method lookup for
special operator methods is different in new style classes, but why?
This might also be related to issue bpo-643841 but I am not sure.
"""
These functions are only called if the left operand does not support the
corresponding operation and the operands are of different types.
For operands of the same type, it is assumed that if the non-reflected
method (such as __add__()) fails the operation is not supported, which
is why the reflected method is not called.
"""
So I'd say the opposite: this is an old-style class problem.
Ah yes, I did not realize the "different types" part. So it is indeed an
old-style class problem, which should behave just like the new-style
classes but they don't.
However I would probably not fix this in the 2.x series of Python.
Changing the behaviour would break compatibility. The documentation
should mention the different behaviour though.
calvinmannequin
changed the title
New style classes __ror__() operator overloading problem
New style vs. old style classes __ror__() operator overloading
Feb 14, 2008
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: