[Python-ideas] [Python-ideos] Dedicated overloadable boolean operators

Michael Selik mike at selik.org
Tue Nov 24 16:42:24 EST 2015


Interesting. To allow rich logical operation in this manner, we would need
to relax the requirement that ``__bool__`` returns a ``bool``. What
negative effects would that have?

On Tue, Nov 24, 2015 at 4:24 PM Greg Ewing <greg.ewing at canterbury.ac.nz>
wrote:

> Brendan Barnwell wrote:
> > On 2015-11-23 22:34, Random832 wrote:
> >
>  >> C# allows overloading the short-circuit operators
> >>
> >> a and b: a if a.__false__() else a & b
> >> a or b: a if a.__true__() else a | b
> >>
> >     The problem is that this kind of overriding doesn't handle the main
> > use case, which is elementwise and-ing/or-ing.
>
> Maybe we can hook into __bool__ somehow, though?
>
> Suppose 'a and b' were treated as:
>
>     try:
>        result = bool(a)
>     except IDoNotShortCircuit:
>        result = a.__logical_and___(b)
>     else:
>        if not result:
>           result = b
>
> Since a __bool__ call is required anyway, this
> shouldn't slow down the case where there is no
> overriding.
>
> --
> Greg
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151124/81a5236d/attachment.html>


More information about the Python-ideas mailing list