[issue37427] sorted, list.sort reject non-boolean objects with __bool__() as `reverse` parameter

Terry J. Reedy report at bugs.python.org
Fri Jun 28 18:04:26 EDT 2019


Terry J. Reedy <tjreedy at udel.edu> added the comment:

Not a bug.  Both function docs say "reverse is a boolean value."
https://docs.python.org/3/library/stdtypes.html#boolean-values
says "Boolean values are the two constant objects False and True."  These should be used in new code.

For compatibility with old code, CPython generally allows 0 and 1.  In this case, it allows any 'integral value', but I would not necessarily expect this of other implementations.

[In general, the *Python language* docs do not document extra latitude allowed by the *CPython implementation*.  So the lack thereof here is also not a bug.]

All objects without fancy trickery have a __bool__ method, either inherited from *object* or overridden.  This in no way makes all such objects, including Xs, into boolean values.

----------
nosy: +terry.reedy
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed
title: sorted() and list.sort() don't accept non-boolean objects with __bool__() as `reverse` parameter -> sorted, list.sort reject non-boolean objects with __bool__() as `reverse` parameter

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37427>
_______________________________________


More information about the Python-bugs-list mailing list