[Python-ideas] Revisiting dedicated overloadable boolean operators

Steven D'Aprano steve at pearwood.info
Mon Aug 6 21:02:30 EDT 2018


On Mon, Aug 06, 2018 at 02:44:24PM -0700, Neil Girdhar wrote:

> This doesn't work because the logical Boolean operators short circuit in 
> Python.  So you could not even define these operators for the regular 
> Python types.

Todd is not proposing to add dunder methods for the existing "or" and 
"and" operators.

Todd is proposing four new operators spelled "bAND", "bOR", "bXOR" and 
"bNOT", which aren't short-circuiting and call dunder methods, just like 
other operators including "in".

You seem to be saying that "this" (defining new operators that call 
dunder methods) doesn't work because a set of *completely different* 
existing operators short-circuit. If that's not what you meant, I 
don't understand what you actually did mean.


> Your two examples numpy and SQLAlchemy don't want this 
> short-circuiting behavior, so you would never want to write anything like
> 
> (some_array or some_other_array)
> 
> The reader of this code might imagine that there is some short circuiting 
> or conversion to Boolean.

Fortunately Todd isn't proposing that.


-- 
Steve


More information about the Python-ideas mailing list