[Python-ideas] PEP 505: None-aware operators

MRAB python at mrabarnett.plus.com
Mon Jul 23 20:21:27 EDT 2018


On 2018-07-23 23:05, Giampaolo Rodola' wrote:
> On Mon, Jul 23, 2018 at 6:53 PM Steven D'Aprano <steve at pearwood.info> wrote:
>>
>> On Mon, Jul 23, 2018 at 02:04:17PM +0200, Giampaolo Rodola' wrote:
>> > "a?.b" does two and that's a fundamental difference (explicitness).
>>
>> How is "two things" less explicit than "one thing"?
>> Comments like the above is why I think that "explicit" and "implicit"
>> are used to mean "I like it" and "I don't like it" rather than being
>> objective arguments, or indeed having anything to do with explicitness
>> or implicitness.
> 
> This:
> 
>      v = a?.b
> 
> ...*implicitly* checks if value is not None [and continues execution].

It's no more implicit than 'or' checking for falseness.

> This:
> 
>      v = a
>      if a.b is not None:
>          v = a.b
> 
> ...*explicitly* checks if value is not None and continues execution.
> If for some reason '?'[ is also going to swallow LookupError then
> *that* would further decrease explicitness, because LookupError would
> be nowhere in sight, the same way "if", "is", "not", "None", ":", "new
> line" are nowhere in sight in the 'a?.b' example. Some argued "A ?? B"
> is less explicit than "A if A is not None else B" for the same reason.
> One may argue that silently returning None instead of raising
> AttributeError is also less explicit.
> This - and this only - is my argument about explicitness. It doesn't
> have to do with how many things are hidden behind an import statement
> or what happens on sorted() (that's comparing apples and oranges).
> I hope it's clear now.
> 
>> > It
>> > does so by introducing a brand new operator ("?") which can be spelled
>> > in two forms ("a?.b" and "a?[b]") by using two adjacent symbols not
>> > interrupted by any space, which is an absolute first in the Python
>> > syntax
>>
>> It isn't a first. Many existing operators use two adjacent symbols not
>> interrupted by a space:
>>
>> e.g.  ==  <=  >=  !=  **  //  << >> +=  -=  *= etc.
> 
> You say 'a == b'. You can't say 'a ?. b' (not that it matters, it
> would be less intuitive anyway). You can't because '.?' is the only
> couple of contiguous symbols requiring "something" before and after
> with no spaces in between, and that's a first in the language.

You _can_ say 'a ?. b', just as you _can_ say 'a . b'.

Also, it's not a couple of contiguous symbols, it's a single symbol, 
just as '<=' is a single symbol (and you can't put a space in the middle 
of that either).
[snip]


More information about the Python-ideas mailing list