[Python-Dev] Re: The repr of a sentinel

2021年5月13日 10:47:31 -0700

On 5/13/2021 1:39 PM, Tal Einat wrote:
On Thu, May 13, 2021 at 7:44 PM Ethan Furman <[email protected]> wrote:
Consider me complaining. ;-)
+1
An actual Sentinel class would be helpful:
 >>> class Sentinel:
 ... def __init__(self, repr):
 ... self.repr = repr
 ... def __repr__(self):
 ... return self.repr
 ...
 >>> MISSING = Sentinel('MISSING')
 >>> MISSING
 MISSING
 >>> implicit = Sentinel('<implicit>')
 >>> implicit
 <implicit>
Here is my suggestion (also posted on the related bpo-44123), which is
also simple, ensures a single instance is used, even considering
multi-threading and pickling, and has a better repr:
class Sentinel:
 def __new__(cls, *args, **kwargs):
 raise TypeError(f'{cls.__qualname__} cannot be instantiated')
class MISSING(Sentinel):
 pass
>>> MISSING
<class '__main__.MISSING'>
I think a repr of just "MISSING", or maybe "dataclasses.MISSING" would be better.
Eric
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/EQZV3KZZQNBN6NQRIERUK5HOEAUW2DUJ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to