How can I make the default value of an argument depend on another argument (in Python)?

The language doesn’t support such syntax.

The usual workaround for these situations(*) is to use a default value which is not a valid input.

def func(n=5.0, delta=None):
     if delta is None:
         delta = n/10

(*) Similar problems arise when the default value is mutable.

Leave a Comment