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
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to