Tal Einat <[email protected]> added the comment:
... and they can be given excellent reprs by using a meta-class:
class Sentinel(type):
@classmethod
def __prepare__(cls, name, bases, **kwds):
d = super().__prepare__(name, bases, **kwds)
def __new__(cls_, *args, **kwargs):
raise TypeError(
f'{cls_!r} is a sentinel and cannot be instantiated')
d.update(__new__=__new__)
return d
def __repr__(cls):
return f'{cls.__module__}.{cls.__qualname__}'
class MISSING(metaclass=Sentinel): pass
This also has another nice benefit:
>>> type(MISSING)
<class 'sentinels.Sentinel'>
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue44123>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com