r/Python • • Jun 06 '21

News PEP 661 -- Sentinel Values

https://www.python.org/dev/peps/pep-0661/
220 Upvotes

109 comments sorted by

View all comments

Show parent comment

2

u/baubleglue Jun 06 '21
def get(key, default=None, default_is_missing=False):

if default_is_missing and default is not None:
        raise Exception("invalid parameters") 
    try:
    return get_value_from_somewhere(key)
except ValueNotFoundError:
    if default_is_missing:
            raise
        return default

2

u/BlckKnght Jun 08 '21

That gets the one-argument API wrong. Calling get(invalid_key) should raise, but your example doesn't, you need an extra keyword argument for that (get(invalid_key, default_is_missing=True)). These APIs already exist all over (including dict.get), so you can't just change their logic, that would break all kinds of code!

1

u/baubleglue Jun 08 '21

Dict.get doesn't use any sentinel values. I think once I need for my code something like sentinel and I ended up with some kind of dispatch: if no value use get_no_value instead of get. In any case it is all different versions of making something for missing function overloading. I think about people who learn coding with Python, and here in stdlib a trick is sold as a right way to write code - standard way to handle undefined. Then we will soon have it every as a valid value (like NaN).