From eef6bf5175b3343e860e3454008009264a9ad4c0 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 11 Feb 2020 17:06:42 -0800 Subject: [PATCH] Add sentinel.py. --- voussoirkit/sentinel.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 voussoirkit/sentinel.py diff --git a/voussoirkit/sentinel.py b/voussoirkit/sentinel.py new file mode 100644 index 0000000..48ab59e --- /dev/null +++ b/voussoirkit/sentinel.py @@ -0,0 +1,26 @@ +class Sentinel: + ''' + Sentinel objects are used when you need to have some kind of default, + placeholder, or special value that can't be confused with any other value + in your program. For example, if you are waiting for a function to return a + value, but you use `None` as a placeholder, someone might get confused and + think that the function actually returned None. + + You can get cheap sentinels by just creating plain Python `object()`s, but + they are bad for printing because you'll just see an ID and not know what + the sentinel represents. This simple Sentinel class lets you give them a + name and adjust their truthyness which can be useful. + + Some implementations of sentinels also make them singletons. These ones are + not singletons! Separate sentinels will never == each other even if you use + the same name! + ''' + def __init__(self, name, truthyness=True): + self.name = name + self.truthyness = truthyness + + def __bool__(self): + return bool(self.truthyness) + + def __repr__(self): + return f''