[Python-ideas] PEP 505 (None coalescing operators) thoughts

Eric Snow ericsnowcurrently at gmail.com
Tue Sep 29 15:40:21 CEST 2015


On Tue, Sep 29, 2015 at 3:49 AM, M.-A. Lemburg <mal at egenix.com> wrote:
> On 28.09.2015 23:49, Guido van Rossum wrote:
>> But that's a different point (for the record I'm not a big fan of the ?
>> either).
>
> Me neither.

Same here.

>
> The proposal simply doesn't have the right balance between usefulness
> and complexity added to the language (esp. for new Python programmers
> to learn in order to be able to read a Python program).

+1

>
> In practice, you can very often write "x or y" instead of
> having to use "x if x is None else y", simply because you're
> not only interested in catching the x is None case, but also
> want to override an empty string or sequence value with
> a default. If you really need to specifically check for None,
> "x if x is None else y" is way more expressive than "x ?? y".
>
> For default parameters with mutable types as values,
> I usually write:
>
> def func(x=None):
>     if x is None:
>         x = []
>     ...

I do the same.  It has the right amount of explicitness and makes the
default-case branch more obvious (subjectively, of course) than the
proposed alternative:

def func(x=None):
    x = x ?? []
    ...

>
> IMO, that's better than any of the above, but perhaps that's
> just because I don't believe in the "write everything
> in a single line" pattern as something we should strive
> for in Python.

Yeah, the language has been pretty successful at striking the right
balance here.  IMO, the proposed syntax doesn't pay off.

-eric


More information about the Python-ideas mailing list