Question

I noticed that while the 'max' function do well on None type:

In [1]: max(None, 1)
Out[1]: 1

'min' function doesn't return anything:

In [2]: min(None, 1)
In [3]: 

Maybe it's because there is no definition for min(None, 1)? So why in max case, it return the number? Is None treated like '-infinity'?

Was it helpful?

Solution

As jamylak wrote, None is simply not printed by Python shells.

This is convenient because all functions return something: when no value is specified, they return None:

>>> def f():
...     print "Hello"
...     
>>> f()
Hello
>>> print f()  # f() returns None!
Hello
None

This is why Python shells do not print a returned None value. print None is different, though, as it explicitly asks Python to print the None value.


As for comparisons, None is not considered to be -infinity.

The general rule for Python 2 is that objects that cannot be compared in any meaningful way don't raise an exception when compared, but instead return some arbitrary result. In the case of CPython, the arbitrary rule is the following:

Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.

Python 3 raises an exception, for non-meaningful comparisons like 1 > None and the comparison done through max(1, None).


If you do need -infinity, Python offers float('-inf').

OTHER TIPS

It does return something, the python shell just doesn't print None

>>> min(None, 1)
>>> print min(None, 1)
None

If you really want a value that is always going to compare less than any other, you need to create a little class:

class ValueLessThanAllOtherValues(object):
    def __cmp__(self, other):
        return -1

# really only need one of these
ValueLessThanAllOtherValues.instance = ValueLessThanAllOtherValues()

This class will compare against values of any other type:

tiny = ValueLessThanAllOtherValues.instance
for v in (-100,100,0,"xyzzy",None):
    print(v)
    print(v > tiny)
    print(tiny < v)
    # use builtins
    print(min(tiny,v))
    print(max(tiny,v))
    # does order matter?
    print(min(v,tiny))
    print(max(v,tiny))
    print()

Prints:

-100
True
True
<__main__.ValueLessThanAllOtherValues object at 0x2247b10>
-100
<__main__.ValueLessThanAllOtherValues object at 0x2247b10>
-100

100
True
True
<__main__.ValueLessThanAllOtherValues object at 0x2247b10>
100
<__main__.ValueLessThanAllOtherValues object at 0x2247b10>
100

0
True
True
<__main__.ValueLessThanAllOtherValues object at 0x2247b10>
0
<__main__.ValueLessThanAllOtherValues object at 0x2247b10>
0

xyzzy
True
True
<__main__.ValueLessThanAllOtherValues object at 0x2247b10>
xyzzy
<__main__.ValueLessThanAllOtherValues object at 0x2247b10>
xyzzy

None
True
True
<__main__.ValueLessThanAllOtherValues object at 0x2247b10>
None
<__main__.ValueLessThanAllOtherValues object at 0x2247b10>
None

tiny is even less than itself!

print(tiny < tiny)
True
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top