Do you know that ≥90% Python tutorials of the float()
function are wrong?
Asked
Active
Viewed 59 times
1 Answers
-2
Yes! Many state this:
It is invalid to give strings like
"abc"
,"Hello"
tofloat()
.
No no no! Test this:
Yes! There's actually ∞ and NaN in Python!
But this is invalid for
int()
and complex()
.
So you can write a subclass of float
to let the __truediv__
and __floordiv__
methods return float("inf")
when ZeroDivisionError
is raised and maybe return float("NaN")
if ValueError
is raised in all of the magic math methods!
This is stated in the Python Docs. But ≥70% people don't know it!
P.S. Do you like JavaScript? Now you can get the similar behavior!

wyz23x2
- 298
- 4
- 16
-
1If you need to add additional information to your question then you should [edit](http://meta.stackexchange.com/a/21789/204922) your original question. Please only use the "Post Answer" button if you have found an answer to your question. Additionally, this post appears to be [off-topic](http://stackoverflow.com/help/on-topic) as per *This question does not appear to be about Stack Overflow or the Stack Exchange engine that powers the Stack Exchange network, within the scope defined in the help center*. – CallMePerox May 06 '20 at 11:50