[Python-Dev] Problems with new-style classes and coercion

David Abrahams David Abrahams" <david.abrahams@rcn.com
Mon, 22 Apr 2002 10:43:08 -0500


This appears to be a 2.2.1 bug. Just looking for confirmation or
correction before I put it in the bug tracker:

Python 2.2 (#28, Mar 13 2002, 23:18:18) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> coerce
<built-in function coerce>
>>> class ex(object):
...     def __init__(self, v):
...             self.v = v
...     def __coerce__(self, other):
...             return self, ex(other)
...     def __add__(self, rhs):
...             return ex(self.v + rhs.v)
...     def __repr__(self):
...             return 'ex(' + repr(self.v) + ')'
...
>>> ex(3)
ex(3)
>>> coerce(ex(1),2.3)
(ex(1), ex(2.2999999999999998))
>>> ex(1).__add__(ex(2))
ex(3)
>>> ex(1)+2.3
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 9, in __add__
AttributeError: 'float' object has no attribute 'v'

Thanks,
Dave

+---------------------------------------------------------------+
                  David Abrahams
      C++ Booster (http://www.boost.org)               O__  ==
      Pythonista (http://www.python.org)              c/ /'_ ==
  resume: http://users.rcn.com/abrahams/resume.html  (*) \(*) ==
          email: david.abrahams@rcn.com
+---------------------------------------------------------------+