(dct, key, lazy_val_func, *args, **kwargs)
return val
A variant of dict.set_default that requires a function instead of a value.

    >>> d = dict()
    >>> lazy_set_default(d, 'a', lambda val: val**2, 5)
    25
    >>> lazy_set_default(d, 'a', lambda val: val**3, 6)
    25
    >>> d
    {'a': 25}
A variant of dict.set_default that requires a function instead of a value.
A variant of dict . set_default that requires a function instead of a value .
def lazy_set_default(dct, key, lazy_val_func, *args, **kwargs):
    """
    A variant of dict.set_default that requires a function instead of a value.

    >>> d = dict()
    >>> lazy_set_default(d, 'a', lambda val: val**2, 5)
    25
    >>> lazy_set_default(d, 'a', lambda val: val**3, 6)
    25
    >>> d
    {'a': 25}
    """
    try:
        val = dct[key]
    except KeyError:
        val = lazy_val_func(*args, **kwargs)
        dct[key] = val
    return val
def lazy_set_default ( dct , key , lazy_val_func , * args , * * kwargs ) : try : val = dct [ key ] except KeyError : val = lazy_val_func ( * args , * * kwargs ) dct [ key ] = val return val