[Python-ideas] PEP 505 (None coalescing operators) thoughts
python at mrabarnett.plus.com
Mon Sep 28 22:38:38 CEST 2015
On 2015-09-28 21:06, Guido van Rossum wrote:
> On Mon, Sep 28, 2015 at 1:00 PM, Carl Meyer <carl at oddbird.net
> <mailto:carl at oddbird.net>> wrote:
> On 09/28/2015 01:53 PM, Guido van Rossum wrote:
> > On Mon, Sep 28, 2015 at 12:43 PM, Carl Meyer <carl at oddbird.net <mailto:carl at oddbird.net>
> > Or put differently, that whereas these two are trivially equivalent (the
> > definition of left-to-right binding within a precedence class):
> > foo.bar.baz
> > (foo.bar).baz
> > these two are not equivalent:
> > foo?.bar.baz
> > (foo?.bar).baz
> > Right.
> > I'm having trouble coming up with a parallel example where the existing
> > short-circuit operators break "extractibility" of a sub-expression like
> > that.
> > Why is that an interesting property?
> Because breaking up an overly-complex expression into smaller
> expressions by means of extracting sub-expressions into temporary
> variables is a common programming task (in my experience anyway --
> especially when trying to decipher some long-gone programmer's
> overly-complex code), and it's usually one that can be handled pretty
> mechanically according to precedence rules, without having to consider
> that some operators might have action-at-a-distance beyond their
> Well, if just the foo?.bar.baz part is already too complex you probably
> need to reconsider your career. :-)
> Seriously, when breaking things into smaller parts you *have* to
> understand the shortcut properties. You can't break "foo() or bar()" into
> a = foo()
> b = bar()
> return a or b
Can you break:
result = do_this() if test() else do_that()
into parts without changing its meaning/behaviour?
condition = test()
true_result = do_this()
false_result = do_that()
result = true_result if condition else false_result
The ? 'operators' are syntactic sugar.
> > I guess this is because the proposed short-circuiting still "breaks out
> > of the precedence order" in a way that the existing short-circuiting
> > operators don't. Both member access and indexing are within the same
> > left-to-right binding precedence class, but the new operators would have
> > a short-circuit effect that swallows operations beyond where normal
> > left-to-right binding would suggest their effect should reach.
> > Are there existing examples of behavior like this in Python that I'm
> > missing?
> > I don't know, but I think you shouldn't worry about this.
> I think it's kind of odd, but if nobody else is worried about it, I
> won't worry about it either :-)
> Good idea.
More information about the Python-ideas