Question about 'None'

Alex Martelli aleaxit at yahoo.com
Fri Jan 28 04:20:17 EST 2005


flamesrock <flamesrock at gmail.com> wrote:
   ...
> (The reason I ask is sortof unrelated. I wanted to use None as a
> variable for which any integer, including negative ones have a greater
> value so that I wouldn't need to implement any tests or initializations
> for a loop that finds the maximum of a polynomial between certain x
> values. Anything is greater than nothing, no?)

To have this work reliably across versions of Python (and you mentioned
you need it to work on 2.0 as well as 2.3...) you have to make your own
sentinel class, such as:

class MinusInfinity:
    def __cmp__(self, other):
        if isinstance(other, MinusInfinity): return 0
        else: return -1
minusInfinity = MinusInfinity()

Now, you should be able to use this 'minusInfinity" as ``a variable for
which any integer ... has a greater value'', as you wished to use None.

(Untested code, as I don't have any 2.0 on which to test it anyway).


Alex



More information about the Python-list mailing list