A way to have the default value for a parameter as the default of another function: the Default singleton

If I understand the suggestion correctly, you can achieve the goal for yourself with the following:

Default = type('Default', (), dict(__repr__=lambda self: 'Default'))()

def mysorted(seq, *, key=Default, reverse=Default):
    sorted_kwargs = {}
    if key is not Default:
        sorted_kwargs['key'] = key
    if reverse is not Default:
        sorted_kwargs['reverse'] = reverse
    ...
    return sorted(seq, **sorted_kwargs)

The Default class itself is not useful enough to include in builtins (you could just as easily use ... or Default = object()), there’s no realistic way to add special handling of a Default object into the language itself, and this is quite a niche use case anyway. I don’t think there’s anything actionable here.

3 Likes