[Python-Dev] return type of __complex__
Antoine Pitrou
solipsis at pitrou.net
Sun Oct 21 02:15:54 CEST 2012
On Sat, 20 Oct 2012 00:13:51 +1000
Nick Coghlan <ncoghlan at gmail.com> wrote:
> On Fri, Oct 19, 2012 at 11:08 PM, Antonio Cuni <anto.cuni at gmail.com> wrote:
> > Is that the real intended behavior?
>
> Given the way complex numbers interact with floats generally,
> returning a complex number with no imaginary component as a floating
> point value seems legitimate and the checks in cmath overly strict.
> Otherwise you would get redundancy like:
>
> def __complex__(self):
> return complex(value)
>
> or
>
> def __complex__(self):
> return value + 0j
The redundancy sounds like a non-issue to me, since you can implement
__float__ instead:
>>> class C:
... def __float__(self): return 5.0
...
>>> complex(C())
(5+0j)
>>> cmath.cos(C())
(0.28366218546322625+0j)
Regards
Antoine.
--
Software development and contracting: http://pro.pitrou.net
More information about the Python-Dev
mailing list