adding sentinel metaclass example
This commit is contained in:
24
25-class-metaprog/sentinel/sentinel.py
Normal file
24
25-class-metaprog/sentinel/sentinel.py
Normal file
@@ -0,0 +1,24 @@
|
||||
"""
|
||||
|
||||
>>> class Missing(Sentinel): pass
|
||||
>>> Missing
|
||||
<Missing>
|
||||
>>> class CustomRepr(Sentinel):
|
||||
... repr = '*** sentinel ***'
|
||||
...
|
||||
>>> CustomRepr
|
||||
*** sentinel ***
|
||||
|
||||
"""
|
||||
|
||||
class SentinelMeta(type):
|
||||
def __repr__(cls):
|
||||
try:
|
||||
return cls.repr
|
||||
except AttributeError:
|
||||
return f'<{cls.__name__}>'
|
||||
|
||||
class Sentinel(metaclass=SentinelMeta):
|
||||
def __new__(cls):
|
||||
return cls
|
||||
|
||||
Reference in New Issue
Block a user