[Python-ideas] Null coalescing operators

Random832 random832 at fastmail.com
Sat Sep 19 02:58:50 CEST 2015

On Fri, Sep 18, 2015, at 18:37, Chris Angelico wrote:
> created?.isoformat?() # is equivalent to
> created.isoformat() if created is not None and created.isoformat is
> not None else None

More or less - it'd only look up the attribute once.

> As to the syntax... IMO this needs to be compact, so ?. has my
> support. With subscripting, should it be "obj?[idx]" or "obj[?idx]" ?
> FWIW Pike uses the latter, but if C# uses the former, there's no one
> obvious choice.

?[ has the benefit of being consistent with ?. - and ?(, for that
matter. It actually suggests a whole range of null-coalescing operators.
?* for multiply? A lot of these things are done already by the normal
operators for statically-typed nullable operands in C#.

That could get hairy fast  - I just thought of a radical alternative
that I'm not even sure if I support: ?(expr) as a lexical context that
changes the meaning of all operators.

More information about the Python-ideas mailing list