A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2012-October/122291.html below:

[Python-Dev] return type of __complex__

[Python-Dev] return type of __complex__ [Python-Dev] return type of __complex__Serhiy Storchaka storchaka at gmail.com
Sun Oct 21 10:26:19 CEST 2012
On 21.10.12 03:15, Antoine Pitrou wrote:
> The redundancy sounds like a non-issue to me, since you can implement
> __float__ instead:

 >>> class C:
...     def __init__(self, v):
...         self.v = v
...     def __complex__(self):
...         return self.v**0.5
...
 >>> import cmath
 >>> cmath.cos(C(-1))
(1.5430806348152437-7.195794243779206e-17j)
 >>> cmath.cos(C(1))
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: __complex__ should return a complex object

Returned value can be computed and result can be float or complex 
depending on values. The author of the class with defined __complex__() 
may not even be aware that under certain circumstances he would get a float.

*Always* write "return complex(...)" is too redundant.


More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4