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

0

u/baubleglue Jun 06 '21

> how can get() distinguish between None-as-in-raise-exception and None-as-in-default-value?
None is None is shouldn't be treated differently depends on context it is always possible to pass additional flag parameter none_is_real=True/False. It is ugly but probably less ugly than creating special sentinel. Python doesn't support function overloading, that is the reason we even think about sentinel for arguments.

I understand there is a need for missing value, but local solutions are working. You can unify convention for specific project, but there is no reason to make it a feature - "_sentinel" is OK. "sentinel" is not.

Your version with MissingType has other problems:

  • normal type for a variable is deferent from MissingType (unless we want to embrace Python's dynamic type system)
  • MissingType is alias for Undefined

Formal solution would be to use wrapper class (like Java's java.lang.Long for type long) , but it is probably extremally inefficient in critical cases, maybe named tuple is better alternative?
MyINTValue = namedtuple(
"MyINTType",
["value", "is_missing"],
defaults=(None, False))

v = MyINTValue(None, True) # missing None
v = MyINTValue(None) # real None

v = MyINTValue(1) # normal use case

But again IMHO it should be project level decision and not something promoted as a feature.

2

u/genericlemon24 Jun 07 '21

It is ugly but probably less ugly than creating special sentinel.

Maybe so; nevertheless, it's an established pattern people are already using (see this article from over 10 years ago). Even if they wanted to change, they may not be able to because of backwards compatibility. They would still benefit from the right tools.

Python doesn't support function overloading, that is the reason we even think about sentinel for arguments.

It supports overloading-like behavior, and that's enough; this is acknowledged by the existence of typing.overload.

Your version with MissingType has other problems: [...]

The exact same problems None has when it's not a valid value. None is different from the variable type, that's why you have Optional[VarType], which is an alias for Union[VarType, None]; you can model this in exactly the same way: Union[VarType, MissingType]; here's an example.

Formal solution would be to use wrapper class (like Java's java.lang.Long for type long) , but it is probably extremally inefficient in critical cases, maybe named tuple is better alternative?

Maybe so, but as I said, sentinel objects are an already established pattern.

Also, from the perspective of a user, wrapping all the objects from an iterable in another type is cumbersome. As an API designer, I'd prefer to do the ugly thing myself once, so many users using my library don't have to.

Rust has enums for variables that are a "union" of types (union as in sets, not as in C). I don't why see Python wouldn't have something similar (it has, with Union).

I understand there is a need for missing value, but local solutions are working. You can unify convention for specific project, but there is no reason to make it a feature

Local solutions are not working for the stdlib devs, as explained in the PEP. This is a feature for them (see Abstract).

People can keep using their own sentinels, for old and new projects alike. If they have the same needs as the stdlib, they can use the ones from stdlib, but they don't have to.