sentinel value
A sentinel value is a special value that an algorithm treats as a signal rather than as ordinary data, most often to mark the end of a sequence or to terminate a loop. Its defining requirement is that it stay distinct from every legitimate value the data can take, so a reading loop can check each value and stop the moment the sentinel appears:
Sentinels carry meaning in-band, inside the data stream itself, which lets code detect a boundary when no separate length or count is available.
Common Python examples include the empty string that readline() returns at the end of a file, a sentinel object placed on a queue.Queue to signal that no more items are coming, and a stop word that ends an input loop.
The technique breaks down when no value can be spared as the signal, which leads to the semipredicate problem: the sentinel collides with a legitimate result, so code can no longer tell a signal from real data. That risk is why many languages prefer out-of-band signaling through option types, exceptions, or a unique marker object. In Python, calling object() produces a guaranteed-unique sentinel.
Related Resources
Tutorial
Null in Python: Understanding Python's NoneType Object
In this tutorial, you'll learn about the NoneType object None, which acts as the null in Python. This object represents emptiness, and you can use it to mark default parameters and even show when you have no result. None is a tool for doing everything with nothing!
For additional information on related topics, take a look at the following resources:
- Python while Loops: Repeating Tasks Conditionally (Tutorial)
- Iterators and Iterables in Python: Run Efficient Iterations (Tutorial)
- The Walrus Operator: Python's Assignment Expressions (Tutorial)
- Python for Loops: The Pythonic Way (Tutorial)
- Python's map(): Processing Iterables Without a Loop (Tutorial)
- How to Iterate Through a Dictionary in Python (Tutorial)
- Python's None: Null in Python (Course)
- Mastering While Loops (Course)
- Python while Loops: Repeating Tasks Conditionally (Quiz)
- Efficient Iterations With Python Iterators and Iterables (Course)
- Iterators and Iterables in Python: Run Efficient Iterations (Quiz)
- Python Assignment Expressions and Using the Walrus Operator (Course)
- The Walrus Operator: Python's Assignment Expressions (Quiz)
- For Loops in Python (Definite Iteration) (Course)
- Python for Loops: The Pythonic Way (Quiz)
- Python's map() Function: Transforming Iterables (Course)
- Python Dictionary Iteration: Advanced Tips & Tricks (Course)
- Python Dictionary Iteration (Quiz)