This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2008年02月13日 14:51 by calvin, last changed 2022年04月11日 14:56 by admin. 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 |
| 2022年04月11日 14:56:30 | admin | set | github: 46361 |
| 2010年06月17日 02:08:01 | terry.reedy | set | status: open -> closed resolution: out of date |
| 2008年12月04日 15:17:03 | fdrake | set | assignee: fdrake -> |
| 2008年03月19日 20:47:21 | jafo | set | priority: normal assignee: fdrake components: + Documentation, - Interpreter Core nosy: + fdrake |
| 2008年02月14日 07:16:03 | calvin | set | messages:
+ msg62379 title: New style classes __ror__() operator overloading problem -> New style vs. old style classes __ror__() operator overloading |
| 2008年02月13日 15:01:24 | amaury.forgeotdarc | set | nosy:
+ amaury.forgeotdarc messages: + msg62362 |
| 2008年02月13日 14:51:15 | calvin | create | |