I recently came across the same issue with a slightly different use case:
I had a class with a flag attribute whose value is passed to the caller __init__ . Class objects can be created from two different versions of the data, where the older version of the data does not contain the information necessary to determine whether the True False flag should.
Setting otherwise bool-value to None (which is the usual way of representing missing data) will not work, because None happily evaluates False .
Like you, I did not find a satisfactory built-in solution, so I wrote it myself.
(written for python2.7 but easy to configure fo python3)
class NotTrueNorFalseType(object): """ A singleton class whose instance can be used in place of True or False, to represent a value which has no true-value nor false-value, eg a boolean flag the value of which is unknown or undefined. """ def __new__(cls, *args, **kwargs):
The constructive (minimal) solutions in this class were inspired by the None singleton (for example, by naming the singleton instance "Foo" and the class "FooType", returning "Foo" from __repr__ , raising TypeError for invalid operations).
shx2 Aug 15 '14 at 16:29 2014-08-15 16:29
source share