operator overloading

Terry Reedy tjreedy at udel.edu
Wed Apr 4 16:55:58 EDT 2007


"Ziga Seilnacht" <ziga.seilnacht at gmail.com> wrote in message 
news:1175711246.488717.262560 at e65g2000hsc.googlegroups.com...
| This looks like a bug in Python. It works for all the other
| operators:
|
| >>> class MyInt(int):
| ...     __sub__ = int.__add__
| ...     __mul__ = int.__add__
| ...     __div__ = int.__add__
| ...     __truediv__ = int.__add__
| ...     __floordiv__ = int.__add__
| ...     __mod__ = int.__add__
| ...     __lshift__ = int.__add__
| ...     __rshift__ = int.__add__
| ...     __and__ = int.__add__
| ...     __xor__ = int.__add__
| ...     __or__ = int.__add__
| ...     __pow__ = int.__add__
| ...
| >>> i = MyInt(42)
| >>> i + 3
| 45
| >>> i - 3
| 45
| >>> i * 3
| 45
| >>> i / 3
| 45
| >>> i // 3
| 45
| >>> i % 3
| 45
| >>> i << 3
| 45
| >>> i >> 3
| 45
| >>> i & 3
| 45
| >>> i ^ 3
| 45
| >>> i | 3
| 45
| >>> i ** 3
| 74088
|
| You should submit a bug report to the bug tracker:
|
| http://sourceforge.net/bugs/?group_id=5470

Nice test.  I thought maybe __pow__ might be different in not having a 
reverse form, but indeed, int has an __rpow__ method.

Before submitting, make sure that this does not work in 2.5, and then say 
so in the bug report.  In fact, copy the version/system info that the 
interactive interpreter puts up when it starts.

tjr










More information about the Python-list mailing list