[Python-ideas] Fwd: Null coalescing operator

MRAB python at mrabarnett.plus.com
Sun Sep 11 21:06:47 EDT 2016


On 2016-09-12 01:26, Guido van Rossum wrote:
> On Sun, Sep 11, 2016 at 12:44 PM, Daniel Moisset
> <dmoisset at machinalis.com> wrote:
>> Both this discussion, PEP 505, and the one a year ago, tend to mix up 2
>> related but separate proposals:
>
> I don't think there's that much of a mix-up. PEP 505 clearly describes
> each proposal separately and even gives a choice to accept or reject
> each one separately.
>
>> (A) Add a None-coalescing operator (like C# a ?? b, what you would write in
>> Python as "a or b" if it didn't have the falsy gotcha)
>
> https://www.python.org/dev/peps/pep-0505/#none-coalescing-operator
>
>> (B) Add some None-aware navigation operators ( The "?.", "?()", "?[]", or
>> what you would write in python as "a and a.attribute" if it didn't have the
>> falsy gotcha)
>
> https://www.python.org/dev/peps/pep-0505/#none-aware-attribute-access-operator
>
>> Both are things that can be already done in python, so the purpose here is
>> to add some convenience (aka "syntax sugar"). IMO, this kind of syntax sugar
>> proposals should be weighed with the frequency of the coding pattern where
>> the sugar can be applied. And from the stats presented in PEP-505 (B) is one
>> order of magnitude less usual than (A); that matches most of the examples I
>> see in the threads and FWIW my personal experience.
>
> I can't argue here yet. Honestly I looked at some examples; for those
> where it wasn't instantly clear that a None-coalescing operator would
> *not* help, I found it hard to figure out how to rewrite it. E.g. this
> one -- quick, is there a better way?
>
>   return "Illegal Argument" + (self.message is not None and (": " +
> self.message) or "")
>
I think this is better:

     return "Illegal Argument" + ("" if self.message is None else ": " + 
self.message)

but it doesn't require a None-coalescing operator.


More information about the Python-ideas mailing list