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

Peter J. Holzer hjp-python at hjp.at
Sun Jul 22 12:21:35 EDT 2018


On 2018-07-22 09:01:58 -0400, David Mertz wrote:
> On Sun, Jul 22, 2018, 8:11 AM Steven D'Aprano <steve at pearwood.info> wrote:
>     To me, the ?? operator seems like a clear and obvious win. The other
>     variants are more complex and the benefit is not as obvious to me, so I
>     haven't decided where I stand on them.
> 
> 
> Yes, that is the only one I'm merely -0 on. The others I'm -1000.

For me it's the opposite. ?? is +0: While I use the equivalent // in
Perl quite frequently, the benefit isn't all that great.

But ?. and ?[] are really useful.

Sequences like request.context.user.email occur quite frequently in
code, and I find 

    request?.context?.user?.email

much more readable than

    email = None
    context = request.context
    if context is not None:
        user = context.user
        if user is not None:
            email = user.email

Note that 

    request and request.context and request.context.user and request.context.user.email

is not equivalent even if you assume that None is the only possible
falsey value in this context. It evaluates request 4 times,
request.context 3 times, and request.context.user 2 times.

        hp


-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp at hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180722/8ff0b301/attachment.sig>


More information about the Python-ideas mailing list