[Python-ideas] PEP 505: None-aware operators
MRAB
python at mrabarnett.plus.com
Tue Jul 31 21:04:32 EDT 2018
On 2018-07-31 20:53, Jonathan Fine wrote:
> Stephan Houben wrote:
>
>> Nope, the introduction of the tmp variable changed the semantics. It isn't a
>> "chain" anymore so it breaks shortcutting.
>
> I'm confused. Assume 'a' is not defined.
>
> With Python's dot (attribute access) we have
>>>> a.b.c
> NameError: name 'a' is not defined
>>>> a.(b.c)
> SyntaxError: invalid syntax
>>>> (a.b).c
> NameError: name 'a' is not defined
>
> I'd expect, after PEP 505 is added to Python that we'd get
>>>> a ?. b ?. c
> NameError: name 'a' is not defined
>>>> a ?. (b ?. c)
> SyntaxError: invalid syntax
>>>> (a ? . b) ?. c
> NameError: name 'a' is not defined
>
> If this is not correct, please some do tell me what we would get.
>
Correct.
> Now assume 'a' is defined. I'd also expect, for any value for 'a', that
>>>> tmp = (a ?. b)
>>>> val = tmp ?. c
> give val the same value as
>>>> val = (a ?. b) ?. c
>
> If this is not so, then can the second val be computed from tmp? And if so, how?
>
Also correct.
On the first point, Steven said that _figuratively speaking_ (his words)
the series of attribute accesses would be _as though_ it was grouped
a?.(b.c), so if a is None, then remainder of the attributes accessed
would be short-circuited. He _wasn't_ suggesting that that was _actual_
syntax.
On the second point, you'd said:
tmp = spam ?. eggs
val2 = tmp . cheese . aardvark # For spam?.eggs.cheese.aardvark
i.e. that:
spam?.eggs.cheese.aardvark
could be rewritten as:
tmp = spam ?. eggs
val2 = tmp . cheese . aardvark
Steven pointed out that that was wrong.
In the first, if spam is None, then the remainder is short-circuited,
and so the result of spam?.eggs.cheese.aardvark is None.
In the second, if spam is None, then tmp would be None, and tmp.cheese
would fail.
More information about the Python-ideas
mailing list