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

João Bernardo jbvsmo at gmail.com
Thu Oct 1 23:00:44 CEST 2015


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())

------

# 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:"

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 '??='
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151001/e0d22da0/attachment.html>


More information about the Python-ideas mailing list