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

Michael Selik mike at selik.org
Thu Jul 19 19:57:16 EDT 2018


On Thu, Jul 19, 2018 at 5:29 PM Steve Dower <steve.dower at python.org> wrote:

> * "It makes it more complex"
> * "It's harder to follow the flow"
>
> Depends on your measure of complexity. For me, I prioritise "area under
> the indentation" as my preferred complexity metric (more lines*indents
> == more complex), as well as left-to-right reading of each line (more
> random access == more complex). By these measures, ?. significantly
> reduces the complexity over any of the current or future alternatives::
>
> def f(a=None):
>      name = 'default'
>      if a is not None:
>          user = a.get_user()
>          if user is not None:
>              name = user.name
>      print(name)
>


You left one out, I think, that looks decent. I converted the print to a
return, because I think it's more common to return than print.

    def f(a=None):
        try:
            return a.get_user().name
        except AttributeError:
            return 'default'


def f(a=None):
>      print(a?.get_user()?.name ?? 'none')
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180719/1aba7c33/attachment-0001.html>


More information about the Python-ideas mailing list