[Python-bugs-list] [ python-Bugs-508263 ] complex() doesn't call __complex__

noreply@sourceforge.net noreply@sourceforge.net
Fri, 25 Jan 2002 16:16:37 -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: Sverker Nilsson (svenil)
Date: 2002-01-25 16:16

Message:
Logged In: YES 
user_id=356603

The int() and float() functions worked even though
I returned my own object. I would rather have the
system allow it for complex() too. Otherwise
the same check should be made for the other
functions too.

It just made me loose some time searching for a
possible bug in my program because it was totally
unexpected that it should call float() instead. I
guess I can redefine complex and the others, but
it would be more practical not to have to.

It is not 'surely a bug' to return something else, in
my case I return an expression tree to be evaluated
and code-generated later.

Seems to me the Python code is 'implementing policy',
which I think is bad in general, making unnecessary
checks restricting generality. If it was intended
to catch bugs as implied in the previous response,
it doesnt seems worthwile to restrict operations
for all users only for occasional development bug
catching, the bug would 'surely' be catched later
anyway...

Sverker


----------------------------------------------------------------------

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