I've encountered the same issue, either matching the default values in the else clause (and hoping they won't later be changed) or having to revert to building a kwargs dict, and then in multiple `if` statements, conditionally including arguments.

I've also felt this same pain building dicts with conditionally-included items. I can't recall such pain with lists as illustrated, or with list comprehensions for that matter.

+0 on the suggested syntax. I can't comment on complexity to implement in the interpreter.

 
On Sat, 2021-03-20 at 14:36 -0700, Peter O'Connor wrote:
bump!

On Wed, Jan 13, 2021 at 9:32 AM Peter O'Connor <peter.ed.oconnor@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-leave@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/