Overview¶
Various objects that allow for sentinel-like xsentinels for various purposes, including:
- Easily create your own custom singletons/sentinels.
- Use pre-created ones, such as:
- Default
- Null
Install¶
1 2 3 4 5 |
|
Singleton Object¶
Allows for creation of formal custom special sentinel types,
similar to how None
and NoneType
are like.
Ensures there can only ever be one instance, fi you try to allocate a new one or even try to
copy it, will still be the same single instance, no matter what.
Used for sentinel values Null
, and Default
, which are detailed further below in this readme..
The Singleton class can also easily be used as needed to create true singleton for other purposes than just being sentinels.
Singleton Example¶
Here is an example of using Singleton to easily make a safe sentinel object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Null Object¶
A sentinel object modeled after None
called Null
.
Null
evaluates as a False
-like object. Its purpose is for when you need to know
the difference between Null and None, like for a dataclass that represents a JSON document.
If for example one needs to know if the value was absent from the JSON document vs the value being set to Null. For a normal object, its simpler to have the attribute set to a Null or None value then it is to remove the attribute entirely from the object that represents this theoretical JSON document.
Null Example¶
Here is an example of using Null
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
In the above example, we have a class that can have None or Null values, which control how the object is in-turn represented in a json-dict value of its self.
Default Object¶
A sentinel object modeled after None
called Default
.
Default
evaluates as a False
-like object. Its purpose is for when you need to know
if the caller set None on a method param or attribute vs not setting anything at all.
In other-words, if you need to know the difference between the user supplying a value of None
or not supplying a value in the first place.
When the user does not supply any value for an attribute or method parameter, the underlying code that uses that value may want to supply a sensible default value to use instead.
The Default
sentential value can help you do this easily.
Default Example¶
Here is an example of using Default
:
1 2 3 4 5 6 7 8 9 10 |
|
The above example will attempt to lookup a default value via a environmental variable
if the user does not pass anything into the function (ie: value for some_param
is still at Default
).
Utilites¶
unwrap_union¶
You can pass a type/type-hint object to unwrap_union
to get information about if it contains
a Null or Optional type, along with the non-null/default type.
When you do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|