[Python-bugs-list] [ python-Bugs-508263 ] complex() doesn't call __complex__
noreply@sourceforge.net
noreply@sourceforge.net
Fri, 25 Jan 2002 12:00:20 -0800
Bugs item #508263, was opened at 2002-01-24 18:22
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=508263&group_id=5470
Category: Python Interpreter Core
Group: Python 2.2.1 candidate
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Sverker Nilsson (svenil)
Assigned to: Nobody/Anonymous (nobody)
Summary: complex() doesn't call __complex__
Initial Comment:
It calls __float__ instead.
It is the same in 2.2b+ from Nov 7 and
the one I got from CVS just now. /Sverker
Python 2.2+ (#1, Jan 25 2002, 03:54:26)
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
Type "help", "copyright", "credits" or "license" for
more information.
>>> import comptest
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "comptest.py", line 6, in ?
b = complex(a)
AttributeError: Tst instance has no attribute
'__float__'
>>>
[1]+ Stopped ./python
nicosys [25] cat comptest.py
class Tst:
def __complex__(self):
return self
a = Tst()
b = complex(a)
----------------------------------------------------------------------
>Comment By: Martin v. Löwis (loewis)
Date: 2002-01-25 12:00
Message:
Logged In: YES
user_id=21627
Michael is exactly right. Python *does* call the __complex__
method of the object, as demonstrated in the attached a.py.
If the result is not a complex object, it will try to
convert it to a float, and consider this float to be the
real part of the result. This is what gives the error in
your example.
----------------------------------------------------------------------
Comment By: Michael Hudson (mwh)
Date: 2002-01-25 01:47
Message:
Logged In: YES
user_id=6656
Um. Why does __complex__ return self? It has to return a
complex number, surely?
complex() calls __complex__, which doesn't return a complex
number, so it falls back to trying to convert it to a float
(which would then be promoted to complex), but this fails.
It's possible this is silly behaviour -- a
non-complex-returning __complex__ method is surely a bug and
should be flagged as such.
----------------------------------------------------------------------
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=508263&group_id=5470