Have you seen PEP 505? https://www.python.org/dev/peps/pep-0505/
I'm still praying they add "??=" every time I need it.
Maybe if someone proposed just that operator, it would go a long way
towards simplifying code without resulting in endless debates?
Best,
Neil
On Sunday, May 17, 2020 at 3:23:59 PM UTC-4, James Lu wrote:
>
> Many a python programmer have tired to see code written like:
>
> def bar(a1, a2, options=None):
> if options is None:
> options = {}
> ... # rest of function
>
> syntax if argument is not passed, evaluate {} and store to options
> def foo(options:={}): pass
> syntax if argument is not passed or is None, evaluate {} and store to
> options*
> def foo(options?={}): pass
>
> The Zen of Python states "there shouldn't be two ways to do the same
> thing."
>
> Thus, only one of ":=" or "?=" should be adopted. They should be evalued
> on:
> - Which encourages writing better code?
> - Which catches mistakes easier?
>
> Do we want to encourage callers to pass None to indicate default
> arguments?
>
> spam = { data: True } if arg else None
> bar(a1, a2, param=spam)
>
> versus
>
> bar(a1, a2, { data: True }) if arg else bar(a1, a2)
>
> versus
>
> _ = foo.curry(a1, a2)
> _({data: True}) if arg else _(a1, a2)
>
> Since Python is a strongly typed language, it seems more consistent to me
> that
> this code should throw an error:
> def getoptions():
> ... # code to get options
> # whoops! missing return statement
> #return os.environ
> foo(a1, a2, param=getoptions())
>
> := should be adopted because it catches mistakes more quickly.
>
> On the other hand, ?= replaces the "if kwarg is not None: kwarg = ..."
> idiom.
>
> (I propose adopting only ":=". I show "?=" as a strawman.)
> _______________________________________________
> Python-ideas mailing list -- [email protected] <javascript:>
> To unsubscribe send an email to [email protected] <javascript:>
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/[email protected]/message/MILIX6HSW3PRUNWWP6BN2G2D7PXYFZJ7/
>
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/REIWV2QL42JVLJYLRBPCAINRJYMDWF6H/
Code of Conduct: http://python.org/psf/codeofconduct/