Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Python's bool sorting defined?

Is the ordering of True and False well defined in Python, or is it left as an implementation detail?

From the console, I'm seeing False sort before True...but I don't know if that's a behavior I should rely on or not.

(I'm sure there's some Python doc about this, but I can't find it...)

like image 862
Richard Levasseur Avatar asked Nov 13 '12 00:11

Richard Levasseur


People also ask

Is bool data type in Python?

The Python Boolean type is one of Python's built-in data types. It's used to represent the truth value of an expression. For example, the expression 1 <= 2 is True , while the expression 0 == 1 is False .

What does bool () do in Python?

Python bool() Function The bool() function returns the boolean value of a specified object.

What is boolean sort?

Bools are sorted from false to true. The descending sort will order them from true to false. True is essentially equal to 1, and false to 0. Use boolean sorting for selection algorithms where some objects should be demoted.

What kind of sort does Python use?

Python's default sort uses Tim Sort, which is a combination of both merge sort and insertion sort.


1 Answers

http://docs.python.org/2/reference/datamodel.html#the-standard-type-hierarchy

Booleans: These represent the truth values False and True. The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of plain integers, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.

This reads to me that the python language requires False < True, False == 0, True == 1, True != 2.

The same wording is retained in Python 3 as well.

like image 93
Bill Lynch Avatar answered Sep 24 '22 20:09

Bill Lynch