→ Fairly right.
The class is actually implementing boolean logic while keeping a link to an object (so it is actually a “linked bool”). It should stick to this bare minimum functionality according to the single-responsibility principle. Furthermore, creating objects with non-trivial behavior is footgun.
(Side note : any function returning a bool could be passed at init and called within __bool__()
so it could extends the usage scope outside of None assessment.)
Signature should be akin to
LinkedBool(object, f=lambda x: x is not None)
and the class should have a get
method.
Thus the user could use the natural boolean operators and get the selected object back with a syntax like (quick and dirty example) :
nn = LinkedBool
f_el = lambda l: l == [] # bool function for empty list assessment
selected_object = (nn(A) | nn(B) & not nn(C) ^ nn(D, f=f_el)).get()
→ now the A ?? B
has become (nn(A) | nn(B)).get()
But I think allowing to perform direct operations between these objects and other types will lead to some debugging hell at some point.