[Python-ideas] Using "||" (doubled pipe) as the null coalescing operator?
John Wong
gokoproject at gmail.com
Fri Sep 25 01:13:29 CEST 2015
I just read the PEP. As a user, I would prefer || until...
[Nick]
>
> If this particular syntax were to be chosen, I also came up with the
> following possible mnemonics that may be useful as an explanatory
> tool:
> "||" is a barrier to prevent None passing through an expression
> "!|" explicitly allows None to pass without error
I am like eating my own words, !| is pretty hard to read, especially during
code review. The two symbols look too similar. Do we really need to have
one doesn't raise exception and one that does?
Next, the example title?.upper() in the PEP, this is also kind of ugly and
unclear to me what's the purpose. I do appreciate the idea of circuit, but
I don't feel the syntax is right. To me this is the debate between
defaultdict and primitive dict (but in that debate you don't have the
option to raise or not raise exception, but Nick's proposal does).
> data = [] if data is None else data
Looks like a valid case for a short-cut operator. The argument that
"undesirable effect of putting the operands in an unintuitive order" is not
so bad. Once you have seen it once it should make sense. I will probably
poke Star War and say "python awesome, it is" and our brain will adopt. At
least that line is still readable.
> data = data ?? []
I would prefer || again simply because of no new syntax. Actually, the
price computing example in PEP 505 is not too convincing from a contract
standpoint . The proposal is shorter than writing if requested_quanity is
not None, but if you have to think about using null coalescing operator,
then aren't you already spotting a case you need to handle? The example
shows how the bug can be prevented, so maybe requested_quanlity should
really default to 0 from the beginning, not None. None shouldn't appear and
if it appear it should be a bug, and using null coalescing in this very
example is actually a bug from my view. You are just avoiding ever having
to think about taking care of such case in your code. But then you have
negative number to avoid too... so that still require a sanity check
somewhere.
Just my four cents.
On Thu, Sep 24, 2015 at 6:40 PM, Erik <python at lucidity.plus.com> wrote:
> On 24/09/15 23:30, Guido van Rossum wrote:
>
>> Using "?" as a (pro)noun is even worse than using it as an
>> operator/modifier.
>>
>
> That was what I was trying to say, but you did it more correctly and using
> far fewer characters. How very Pythonic of you ... ;)
>
>
> E.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150924/e479c18e/attachment-0001.html>
More information about the Python-ideas
mailing list