Proposal: min(None, x) and max(None, x) return x
Terry Reedy
tjreedy at udel.edu
Fri Nov 22 10:06:15 EST 2002
"Eric Brunel" <eric.brunel at pragmadev.com> wrote in message
news:arle6m$aa9$1 at news-reader11.wanadoo.fr...
> Hi all,
>
> I quite often find myself doing things like:
>
> xMax = None
> for -whatever-:
> -big calculation leading to a value of x-
> if xMax is None:
> xMax = x
> else:
> xMax = max(x, xMax)
>
> or the same kind of stuff with min, of course.
>
> So I told myself: wouldn't it be great if max(None, x) or min(None,
x)
> always simply returned x? So I could just write:
>
> xMax = None
> for -whatever-:
> -big calculation leading to a value of x-
> xMax = max(x, xMax)
>
> Okay, it only saves 3 lines, but I personally find it quite
natural... In
> addition, today, min(None, x) and max(None, x) work, but have
strange
> results: as far as I can see, min(None, x) is always None and
max(None, x)
> is always x, but this behaviour isn't documented.
>
> Opinions anyone?
What you want are identity elements for the min and max operators
(like 0,1 are for +,*), which are -+ infinity. In a particular
context, you may be able to use finite values for the same purpose.
On some builds (depending on underlying C library), you can actually
get and use values representing and acting like +-infinity. On AS 2.2
Windows build:
>>> Infplus=1e300/1e-300
>>> Infplus
1.#INF
>>> max(1e300,Infplus)
1.#INF
>>> max(-1e300,Infneg)
-1.0000000000000001e+300
>>> Infneg=-1e300/1e-300
>>> Infneg
-1.#INF
>>> min(-1e300,Infneg)
-1.#INF
>>> max(-1e300,Infneg)
-1.0000000000000001e+300
So proposal unnecessary on this build. What you say in last para is
that None is treated as -INFINITY, which is both arbitrary and a bit
weird.
Terry J. Reedy
More information about the Python-list
mailing list