[Python-ideas] In-place assignment for "boolean or"?

David Mertz mertz at gnosis.cx
Mon Mar 26 10:44:33 EDT 2018


I actually HAVE used this idiom moderately often.  But unless you use
overly long names, the current spelling is no trouble at all.

In fact, it's not clear why your function is spelled as it is rather than
with boolean shortcutting (assuming you like the shortcutting
aesthetically).

On Mon, Mar 26, 2018 at 6:56 AM, Cammil Taank <ctaank at gmail.com> wrote:
>
> However, for completeness of the discussion, I want to indicate the
> primary intended use case:
>

    def fn(x=None):
        if not x:
            x = some_complex_or_runtime_dependent_result()

    def fn2(x=None):
        x = x or some_complex_or_runtime_dependent_result()

It looks like the boolean shortcutting version makes a line one character
longer; but it does save a line and an indent level.  Of course, if the
'not x' suite contains multiple statements, you need the 'if' either way.

If, as you write, the condition is actually 'x is not None', you want the
suite for sure.  That's much better than a contorted ternary IMO.  There
were a couple PEPs about "None-shortcutting" for this not-so-special case
of comparing with None.  But also IMO, they all looked incredibly ugly for
little benefit (lots of non-obvious syntax characters that look like Perl
or APL)


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180326/8050d7b3/attachment.html>


More information about the Python-ideas mailing list