[Python-ideas] PEP 505 (None coalescing operators) thoughts

Andrew Barnert abarnert at yahoo.com
Thu Oct 1 23:45:22 CEST 2015


On Oct 1, 2015, at 14:00, João Bernardo <jbvsmo at gmail.com> wrote:
> 
> I've been thinking of practical uses for this new None aware operator and although I really like the idea, I believe this may become too weird to use if some things are not taken care of.
> Are all of those things meant to work? (or am I just crazy?) -- assuming C#-like syntax:
> 
> 
> for el in re.match(foo, bar)?.groups() ?? []:
>     print(el.upper())

Yes.

> 
> ------
> 
> # Assuming this function for readability
> def is_useful(x):
>     return x is not None
> 
> if not foo?: # Should read like "not is_useful(foo)"
>     print('foo is None')
>     # With this syntax it is easier to correct all those pieces of code "if not x:" where it should be "if x is None:"

No. Neither this, nor any of your other examples below, are meant to work.

The proposal adds a binary operator ??, three primary operations ?., ?[], and ?(), and possibly a new assignment ?=.

It does not add a unary postfix operator ?, and you can't infer one from any of those forms any more than you can infer a unary postfix + from +=.

While there was some discussion very early in the thread about whether a unary postfix ? operator could be used instead of all these separate things, but it was quickly realized that it doesn't express short-circuiting right for null-conditional access, doesn't make any sense at all for binary null coalescing, and does the wrong thing for null-conditional assignment, so Guido soundly rejected it and nobody disagreed.

Guido also raised the question of whether we should uptalk most operators, skipping only the ones that make no sense, or only update a few that have compelling use cases. People had a bit of fun exploring the former, but I think everyone agreed that the latter makes a lot more sense, and that's what's in the preliminary PEP.

So, under the proposal under discussions, this example, and your other examples, are all syntax errors, just like "if foo+:" and "if foo++:" are syntax errors.

> if foo??:  # A shorter version possibly meaning "foo ?? True" or "foo is None"
>     print('foo is still None')
> 
> -----
> # Are operators other than [] and () allowed?

> bar = foo? + 1  # For "foo?.__add__(1)" it would work, but for the realistic 
>                 # "type(foo)?.__add__(foo, 1)" it does not translate correctly
> bar = foo ?+ 1  # Another spelling. Is this any better??
> 
> bar = foo? / baz? / 2  # How would this work? '(foo / baz) / 2' or 'foo / (bar / 2)'
> 
> -----
> 
> Also for correctness, the shouldn't the '?=' operator be '??='
> 
> 
> 
> 
> 
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151001/d779e207/attachment-0001.html>


More information about the Python-ideas mailing list