This is, more or less, the syntax added in Nick's PEP 531 draft
. The reddit discussion
about it raised some pretty major concerns about clarity, and I have to admit, I think if you're learning Python as a first language, the ?and, ?else, x?.attr, etc syntax is likely to be very confusing. For me personally, combining a new operator "?" with existing keywords like "and" or "else" just does not make any intuitive sense. I definitely see the value, though, in particular of None-severing, especially as a tool to explicitly specify which attr can be missing -- ie, disambiguating which attribute is missing in a foo.bar.baz lookup (the alternative to which is nested try: except AttributeError: blocks, which gets very messy very quickly). I'm on board with the idea, and I can absolutely imagine using it in my code, but I disagree on the spelling.
A thought I had (perhaps more readable in a reddit comment
) is to condense everything into a single "?" symbol, used for:
+ Coalescing binary operator: foo ? bar
+ Coalescing augmented assignment operator: foo ?= bar
+ Severing unary operator: ?fooPseudocode binary operator examples:
Pseudocode augmented examples:
>>> foo_exists ? bar_never_evaluated
>>> foo_missing ? foo_exists
>>> foo_missing ? bar_missing
Pseudocode unary examples:
>>> foo_exists = 'foo'
>>> foo_exists ?= bar_never_evaluated
>>> foo_exists == 'foo'
>>> foo = Missing
>>> bar_exists = 'bar'
>>> foo ?= bar_exists
>>> foo == 'bar'
>>> foo = None
>>> bar_missing = Missing
>>> foo ?= bar_missing
>>> foo == None
AttributeError: <foo_exists.bar> object has no attribute 'baz_missing'
I personally think that's substantially more readable, but I suppose that's at least somewhat a matter of personal preference.