Files
example-code-2e/25-class-metaprog/sentinel/sentinel.py
2021-05-23 22:19:15 -03:00

24 lines
426 B
Python

"""
>>> class Missing(Sentinel): pass
>>> Missing
Missing
>>> class CustomRepr(Sentinel):
... repr = '<CustomRepr>'
...
>>> CustomRepr
<CustomRepr>
"""
class SentinelMeta(type):
def __repr__(cls):
try:
return cls.repr
except AttributeError:
return cls.__name__
class Sentinel(metaclass=SentinelMeta):
def __new__(cls):
return cls