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)