[Python-ideas] PEP 505: None-aware operators

Elazar elazarg at gmail.com
Thu Jul 26 02:08:17 EDT 2018


Just wanted to note another possibility (most likely to be added to the
"rejected solutions" section).

Add a prefix "!" operator, which will work similarly to the
iterator-unpacking operator "*", but for None.
The expression "[!x]" is equivalent to the expression "[] if x is None else
[x]".

This can be combined with the "or" operator, now working as expected since
it's about lists, and a simple "first" or "single" library function.

Example:

    optdict = dict(encoding=single([!encoding] or
sys.getdefaultencoding()), css=options.css)

It is more verbose than the proposed "??" operator (which I personally
like), but maybe it's less line noise.
One can use it with simple parens and get a tuple, "(!x) or y". note that
"(!x or y)" is a syntax error in this case.


Another, related idea: instead of "??" use a double-token operator "! or":

    value = ham! or spam! or eggs

    optdict = dict(encoding=encoding! or sys.getdefaultencoding(),
css=options.css)

Elazar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180725/c6077c7d/attachment.html>


More information about the Python-ideas mailing list