First of all, you're comparing immutable value types to dynamic objects which are predicated and processed based on the memory address (e.g. for equality testing, by default). Of course they're going to have different semantics, especially when converting to a unique string representation.
A better question to ask is why does it work for *almost* all values of 'float' , but not infinite ones... This is a glaring hole in my opinion.

Why do you think having 'inf' is better than something that means something to the python interpreter? I've had countless times where I've had to Google how to create an infinite float, and second guessed myself because it is very unintuitive to have to call a string conversion to get what amounts to a constant.

The 'use case' is being able to specify a number which is constant, by a constant, and not requiring me to execute a function call. Further, keeping it the way it is harbors readability. Every time I parse code that contains `float('inf')` I ask myself why it is having to do this, because it seems like code shouldn't have to.

Your argument seems to stem from the least possible Python can do (i.e. it doesn't HAVE to do anything more than it does currently). This mailing list is python-ideas, which is for ideas currently not in Python. I am aware that nothing *has* to be done, I am merely suggesting why it would make sense to add, and why it would make Python a more complete, predictable and frankly 'prettier' language (albeit subjective). I don't see how any of your points address those concerns

On Fri, Sep 4, 2020, 10:14 PM Steven D'Aprano <> wrote:
On Fri, Sep 04, 2020 at 09:40:55PM -0400, Cade Brown wrote:
> The `eval(repr(x)) == x` is not a segment of my code; rather it is part of
> Python's description of what 'repr' should do:
> Specifically: ` For many types, this function makes an attempt to return a
> string that would yield an object with the same value when passed to eval()`
> <>

"For many types" and "makes an attempt".

There has never been, and never will be, a guarantee that all objects
will obey that invariant. As I said, it is a Nice To Have, and it is
designed for convenience at the interactive interpreter.

> So everyone in this thread can stop mentioning security concerns; I'm sure
> we're all aware of those and we should instead focus on what repr should do
> and shouldn't do.

You specifically said that math.inf doesn't solve your problem *because*
`eval(repr(x))` doesn't work. Now you are backpeddling and saying that
this is not your actual problem.

(In fact it does work, if you do it correctly.)

There are a million other objects that don't obey that invariant:

    py> x = object()
    py> eval(repr(x)) == x
    SyntaxError: invalid syntax

Why is float infinity so special that it needs to obey the

What's the actual problem, or problems, in your code that you are trying
to solve by making an infinity builtin? If there is no actual problem
being solved, and the only reason you want this is because:

> I think it's weird to not fulfill this promise

you don't have any sympathy from me:

- `eval(repr(x))` is not a promise, it is a mere suggestion
  that *some* types *try* to provide.

- Adding a special built-in constant Infinity just to satisfy
  this Nice To Have feature is overkill.

- It would require float infinities to change their repr from
  'inf' to 'Infinity', and that will break doctests.

- And even if that feature were satisfied by infinity, it
  couldn't be satisfied by float NANs by their very definition:

    py> from math import nan
    py> nan == nan

So while the cost of adding a new Infinity builtin is small, the benefit
is even smaller.

Python-ideas mailing list --
To unsubscribe send an email to
Message archived at
Code of Conduct: