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

Steven D'Aprano steve at pearwood.info
Mon Jul 15 01:43:27 CEST 2013


On 14/07/13 21:18, Joshua Landau wrote:
> On 14 July 2013 12:07, Philipp A. <flying-sheep at web.de> wrote:

>> float('inf') still works, so if your keyboard layout/compose key doesn’t
>> support ∞, you just use the ascii variant.
>
> That would make the code inconsistent.

Who cares if in one function you say float('∞') and in another function you say float('inf') and in a third you say float("inf") [note quotation marks] and in a fourth float("INF")? What possible difference does it make?

If you really care, then refactor all of those calls out to a single constant declared in one place only:

INF = float(random.choice([
         'inf', "inf", "INFINITY", '∞', '+∞', r"""∞""",
         '\N{INFINITY}', "\U0000221e", "\u221E",  # etc.
         ]))

and now you can satisfy everybody's preferred way of writing ∞, no matter what.

And no, I am not serious about calling random.choice. But I am serious about refactoring multiple calls to float(whatever) to a module level constant.



-- 
Steven


More information about the Python-ideas mailing list