Today I learned Python 3.15 will get a new sentinel built-in. Sentinel values are unique placeholder values that are commonly used in programming. Python 3.15 ships with a new built-in sentinel that can be used to create new sentinel values: # Python 3.15+ >>> MISSING = sentinel("MISSING") >>> MISSING MISSING Before this PEP, the most common sentinel idiom used the built-in object: MISSING = object() def my_function(some_arg=MISSING): if some_arg is MISSING: ... # Handle the sentinel In the function above, the sentinel value MISSING is being used to check whether the user passed anything as the parameter some_arg or not. PEP 661, that introduced this built-in, has a great discussion covering the reasons as to why this pattern, and many other sentinel patterns, fall short. In general, each common sentinel idiom suffers from at least one of the following problems: Bad string repr: the string representation is too long and uninformative Type unsafe: the sentinels don't have a distinct…
No comments yet. Log in to reply on the Fediverse. Comments will appear here.