[Python-ideas] Break the dominance of boolean values in boolean context

Nick Coghlan ncoghlan at gmail.com
Tue Sep 13 05:39:38 CEST 2011


On Tue, Sep 13, 2011 at 11:40 AM, Devin Jeanpierre
<jeanpierreda at gmail.com> wrote:
> Can we not allow things like `a < b` to return non-boolean values,
> without altering the behaviour of existing Python types?

We already do. As far as I am aware, the only holdouts are:

x in a (coerces to bool)
not a (coerces to bool)
a and b (doesn't coerce as such, but roughly equivalent to "a if not a else b")
a or b (doesn't coerce as such, but roughly equivalent to "a if a else b")

The first case can already be overridden (via __contains__) and I
don't believe there's any specific reason for retaining the coercion
to bool (aside from nobody writing and championing a patch that
eliminates the restriction). PEP 207 (which added rich comparisons in
the first place) is silent on the matter - it only talks about the
comparison and ordering operations.

The latter 3 cases are discussed in depth in PEP 335 (Overloadable
Boolean Operators). Again, I don't believe there's anything
fundamentally wrong with the idea, but it requires both an up to date
reference implementation and a champion willing to gather use cases,
assess the performance impact and generally argue in its favour. We
don't have the numpy folks knocking down our door asking for
distributable versions of these operations, after all.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list