On Sat, Oct 23, 2021 at 6:20 AM Marc Mueller <cdc...@gmail.com> wrote:
>
> Most of the discussion so far has been focused on (?.). Tbh though, I'm more 
> interested in (??) and (??=). Just reading through code, I constantly notice 
> boilerplate like this which could easily be substituted.
>
> variable = some_function(...)
> if variable is None:
>     variable = []  # some default value
>
> # a bit better with an assignment expression
> if (variable := some_function(...)) is None:
>     variable = []
>
> # or worse with an if expression
> variable = some_function(...) if some_function(...) else []
> # also possible with :=, but not much better
> variable = x if (x := some_function(...)) else []

Bear in mind that these last ones are exactly equivalent to the "or"
operator, as they'll use the default if you have any falsy value.

variable = some_function(...) or []

> # using the coalesce operator would be much more readable IMO
> variable = some_function(...)  ?? []
>
> If (?.) and (?[) are rejected / deferred, maybe there is interest in seeing 
> at least (??) and (??=) through?

I'm actually more interested in a better idiom for non-constant
function default arguments, since that's the place where this kind of
thing often comes up. A nice ??= operator might help if your default
is None, but if you then change the default to be object(), you can't
use ??= any more. As a bonus, the docs for such an argument could
actually say what the default really is:

def bisect_right(a, x, lo=0, hi=len(a), *, key=None): ...

except that it'd need some adornment to say that it's late-bound.

ChrisA
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/DAD32U6CKSWB3HI322WRKRYYKFNWFPEP/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to