bump! On Wed, Jan 13, 2021 at 9:32 AM Peter O'Connor <peter.ed.ocon...@gmail.com> wrote:
> I often find that python lacks a nice way to say "only pass an argument > under this condition". (See previous python-list email in "Idea: Deferred > Default Arguments?") > > Example 1: Defining a list with conditional elements > include_bd = True > current_way = ['a'] + (['b'] if include_bd else [])+['c']+(['d'] if > include_bd else []) > new_way = ['a', 'b' if include_bd, 'c', 'd' if include_bd] > also_new_way = list('a', 'b' if include_bd, 'c', 'd' if include_bd) > > Example 2: Deferring to defaults of called functions > def is_close(a, b, precicion=1e-9): > return abs(a-b) < precision > > def approach(pose, target, step=0.1, precision=None): > # Defers to default precision if not otherwise specified: > velocity = step*(target-pose) \ > if not is_close(pose, target, precision if precision is not > None) \ > else 0 > return velocity > > Not sure if this has been discussed, but I cannot see any clear downside > to adding this, and it has some clear benefits (duplicated default > arguments and **kwargs are the scourge of many real world code-bases) >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/KM2Y3BFF2GDVPS56Z7TX2VMZXJEKKRHZ/ Code of Conduct: http://python.org/psf/codeofconduct/