[Python-ideas] PEP 505 (None coalescing operators) thoughts
Andrew Barnert
abarnert at yahoo.com
Thu Oct 1 07:59:32 CEST 2015
On Sep 30, 2015, at 21:19, Random832 <random832 at fastmail.com> wrote:
>
> Andrew Barnert via Python-ideas
> <python-ideas at python.org> writes:
>
>>> On Sep 30, 2015, at 18:08, MRAB <python at mrabarnett.plus.com> wrote:
>>>
>>> It's only just occurred to me that there's a small inconsistency here.
>>> The "?.", "?[" and "?(" will short-circuit on None, whereas the "??"
>>> will short-circuit on non-None.
>>>
>>> Would that cause any confusion in practice?
>>
>> I noticed this when I was trying to write out grammar, sample ASTs,
>> and sample bytecode for these things. I went searching the thread and
>> saw no one had pointed it out. I went through docs and blogs for other
>> languages, and didn't see anyone pointing out, complaining about, or
>> offering to clear up any confusion. So I figured I wouldn't mention
>> it, and see if anyone else even noticed.
>
> How is it worse than the fact that and short-circuits on true whereas or
> short-circuits on false?
Why are you asking me how it's worse when my conclusion was that it's fine, in the sentence right after the part you quoted?
> For the AST issue, I'm curious as to what you ended up doing about the
> whole-atom_expr nature of the short-circuiting and the fact that ASTs
> don't currently represent an atom_expr as a single object containing a
> list of subscript/attribute/call items?
I posted ?. examples earlier; I don't want to repeat the whole thing (especially after Guido pointed out that it probably wasn't helping anyone who didn't already get it). But briefly, the AST doesn't have to represent the short-circuiting here, any more than it does anywhere else that short-circuits; it just adds an uptalk flag to each Attribute node. At code generation time, any Attribute node that has uptalk=True has a JUMP_IF_NONE to after the primary-or-call (leaving the None attrib value on the stack) after the LOAD_ATTR. (Or, if you don't want to add a new bytecode, it has a string of three ops that do the equivalent.)
The same works for ?[]. For ?(), I'm not sure what the desired semantics are, and for ?? it seemed obviously trivial so I didn't bother.
More information about the Python-ideas
mailing list