Function Defaults

starstar.defaults(func, *a, **kw)[source]

Allow functions to have easily overrideable default arguments. Works with mixed positional and keyword arguments.

This is a wrapper around update_defaults, that also supports default values for unnamed arguments (**kwargs).

defaults(func).update will raise a TypeError if an extra argument is passed, whereas update_defaults will return the extra arguments.

NOTE: This interface is not stable yet.

@starstar.defaults
def abc(a=5, b=6):
    return a + b

assert abc() == 11
abc.update(a=10)
assert abc() == 16
assert abc(2) == 8
starstar.get_defaults(func)[source]

Get the non-empty default arguments to a function (as a dict).

def abc(a=5, b=6):
    return a + b

assert starstar.get_defaults(abc) == {'a': 5, 'b': 6}
starstar.update_defaults(func, **update)[source]

Update a function’s default arguments. Because functions don’t have a mechanism for defining default **kwargs, this will return any parameters not explicitly named in the signature.

TODO: should this be strict?

def abc(a=5, b=6):
    return a + b

assert starstar.get_defaults(abc) == {'a': 5, 'b': 6}

starstar.update_defaults(abc, b=7)

assert starstar.get_defaults(abc) == {'a': 5, 'b': 7}