Planet Python
Follow
Rodrigo Girão Serrão: Sentinel built-in
Python 3.15 introduces a new built-in called sentinel for creating placeholder values. This provides a more idiomatic approach for default arguments or return values when None is not suitable. The sentinel function takes a string argument for its name, which also serves as its default string representation. Optionally, a second named argument can specify a custom representation. It is good practice for the sentinel's name and the variable it's assigned to to match, as linters and type checkers may flag discrepancies. Sentinels often use uppercase names as they are typically unchanging constants. The primary use of sentinels is for comparison, checking if a value matches a specific placeholder. This is useful in functions that need to distinguish between a valid value and an intended absence of one. The "15 days of Python 3.15" series will explore new features with clear examples. An example scenario involves a function called "find" that utilizes sentinels.