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

Chris Angelico rosuav at gmail.com
Mon Jul 23 18:39:34 EDT 2018


On Tue, Jul 24, 2018 at 8:22 AM, Thomas Jollans <tjol at tjol.eu> wrote:
> On 18/07/18 19:43, Steve Dower wrote:
>> When a ``None``-aware operator is present, the left-to-right evaluation
>> may be
>> short-circuited. For example, ``await a?.b(c).d?[e]`` is evaluated::
>>
>>     _v = a
>>     if _v is not None:
>>         _v = _v.b
>>         _v = _v(c)
>>         _v = _v.d
>>         if _v is not None:
>>             _v = _v[e]
>>     await _v
>
> ## NB I only skimmed most of this thread after reading the PEP, so I ##
> ##    apologize if this has been discussed before and I missed it.   ##
>
> I quite like the general idea, but I'm nervous about a rather
> fundamental aspect: This adds a special case in which you can't add
> parentheses to an expression involving a chain of operators to make
> precedence and evaluation order clear.
>
> To use your example,
> a ?? 2 ** b ?? 3   ===  (a ?? 2) ** (b ?? 3) # fine
>
> In the present day,
> a or 2 + 3 * c()   ===   a or (2 + (3 * (c())))
>
> a.b(c).d[e]   ===   (((a.b)(c)).d)[e] # silly, but true.
>
> Short-circuiting doesn't break this. With and and or, the expression
> that's short-circuited away is a self-contained expression in imagined
> (or actual) parentheses.

What about:

5 < x < 10

Can you add parentheses to that to "make precedence and evaluation order clear"?

ChrisA


More information about the Python-ideas mailing list