[Python-ideas] float('∞')=float('inf')

Steven D'Aprano steve at pearwood.info
Sun Jul 14 03:24:21 CEST 2013


On 14/07/13 03:25, Philipp A. wrote:
> so don’t be silly everyone. the question is if float('∞') should work and i
> say “why the hell not”


Firstly, let me say that I personally love the idea of float('∞') working. Or even having literal ∞ recognised as float('inf') (or perhaps that should be Decimal('inf')?).

But I'm still voting -1 on this proposal. If the best argument in favour is "why the hell not?", then whatever benefit we might gain is truly tiny. So tiny that the benefit is probably smaller than the cost. And yes, there is a cost, there is always a cost. There are one-off costs:

- somebody has to program this feature;
- write tests for it;
- write documentation;

and on-going costs:

- that's one more thing for every user to learn;
- programmers will have to take this feature into account whenever they use float.


Now, you might argue, and I will agree, that these costs are probably tiny costs. But the benefit is even tinier.

Cost: tiny, but real;
Benefit: "why the hell not?"
Overall benefit: negative.


Here is a simple implementation that supports ∞, +∞ and -∞.


_float = float

def float(arg):
     if isinstance(arg, str):
         arg = arg.replace('∞', 'inf')
     return _float(arg)


Hands up anyone who already uses this, or something like it, in their code? Anyone? Given how trivial it is to build this functionality if you need it, if you haven't already done so, chances are that you don't need it, even if you think you want it.



-- 
Steven


More information about the Python-ideas mailing list