classification
Title: New style vs. old style classes __ror__() operator overloading
Type: behavior Stage:
Components: Documentation Versions: Python 2.4, Python 2.5
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, calvin, fdrake
Priority: normal Keywords:

Created on 2008-02-13 14:51 by calvin, last changed 2010-06-17 02:08 by terry.reedy. This issue is now closed.

Files
File name Uploaded Description Edit
t.py calvin, 2008-02-13 14:51
Messages (3)
msg62361 - (view) Author: Bastian Kleineidam (calvin) Date: 2008-02-13 14:51
Hi,

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 #643841 but I am not sure.
msg62362 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-02-13 15:01
Doc says:
http://docs.python.org/dev/reference/datamodel.html#object.__ror__

"""
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.
msg62379 - (view) Author: Bastian Kleineidam (calvin) Date: 2008-02-14 07:16
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.
History
Date User Action Args
2010-06-17 02:08:01terry.reedysetstatus: open -> closed
resolution: out of date
2008-12-04 15:17:03fdrakesetassignee: fdrake ->
2008-03-19 20:47:21jafosetpriority: normal
assignee: fdrake
components: + Documentation, - Interpreter Core
nosy: + fdrake
2008-02-14 07:16:03calvinsetmessages: + msg62379
title: New style classes __ror__() operator overloading problem -> New style vs. old style classes __ror__() operator overloading
2008-02-13 15:01:24amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg62362
2008-02-13 14:51:15calvincreate