他の人が言ったように、独自のクラスを定義できます。
class NA_(object):
instance = None # Singleton (so `val is NA` will work)
def __new__(self):
if NA_.instance is None:
NA_.instance = super(NA_, self).__new__(self)
return NA_.instance
def __str__(self): return "NA"
def __repr__(self): return "NA_()"
def __and__(self, other):
if self is other or other:
return self
else:
return other
__rand__ = __and__
def __or__(self, other):
if self is other or other:
return other
else:
return self
__ror__ = __or__
def __xor__(self, other):
return self
__rxor__ = __xor__
def __eq__(self, other):
return self is other
__req__ = __eq__
def __nonzero__(self):
raise TypeError("bool(NA) is undefined.")
NA = NA_()
使用する:
>>> print NA & NA
NA
>>> print NA & True
NA
>>> print NA & False
False
>>> print NA | True
True
>>> print NA | False
NA
>>> print NA | NA
NA
>>> print NA ^ True
NA
>>> print NA ^ NA
NA
>>> if NA: print 3
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 28, in __nonzero__
TypeError: bool(NA) is undefined.
>>> if NA & False: print 3
...
>>>
>>> if NA | True: print 3
...
3
>>>