Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is everything greater than None?

Is there a Python built-in datatype, besides None, for which:

>>> not foo > None True 

where foo is a value of that type? How about Python 3?

like image 559
Attila O. Avatar asked Feb 06 '10 18:02

Attila O.


People also ask

How do you compare None?

Use the is operator to compare to None in Python, e.g. if my_var is None: . The is operator returns True if the two values point to the same object (it checks for identity), whereas the double equals == operator checks if the two values are equal.

Is None vs equal None?

If you want to check whether an object compares equal to None , by all means use obj == None . If you want to check whether an object is None , use obj is None .

Is None and == None Python?

In this case, they are the same. None is a singleton object (there only ever exists one None ). is checks to see if the object is the same object, while == just checks if they are equivalent. But since there is only one None , they will always be the same, and is will return True.

How do you know if a Python is not None?

Use the is not operator to check if a variable is not None in Python, e.g. if my_var is not None: . The is not operator returns True if the values on the left-hand and right-hand sides don't point to the same object (same location in memory).


2 Answers

None is always less than any datatype in Python 2 (see object.c).

In Python 3, this was changed; now doing comparisons on things without a sensible natural ordering results in a TypeError. From the 3.0 "what's new" updates:

Python 3.0 has simplified the rules for ordering comparisons:

The ordering comparison operators (<, <=, >=, >) raise a TypeError exception when the operands don’t have a meaningful natural ordering. Thus, expressions like: 1 < '', 0 > None or len <= len are no longer valid, and e.g. None < None raises TypeError instead of returning False. A corollary is that sorting a heterogeneous list no longer makes sense – all the elements must be comparable to each other. Note that this does not apply to the == and != operators: objects of different incomparable types always compare unequal to each other.

This upset some people since it was often handy to do things like sort a list that had some None values in it, and have the None values appear clustered together at the beginning or end. There was a thread on the mailing list about this a while back, but the ultimate point is that Python 3 tries to avoid making arbitrary decisions about ordering (which is what happened a lot in Python 2).

like image 163
John Feminella Avatar answered Sep 17 '22 15:09

John Feminella


From the Python 2.7.5 source (object.c):

static int default_3way_compare(PyObject *v, PyObject *w) {     ...     /* None is smaller than anything */     if (v == Py_None)             return -1;     if (w == Py_None)             return 1;     ... } 

EDIT: Added version number.

like image 32
Torsten Marek Avatar answered Sep 20 '22 15:09

Torsten Marek