This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author taleinat
Recipients eric.smith, iritkatriel, taleinat, zach.ware
Date 2021-05-13.17:21:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1620926510.3.0.529348939246.issue44123@roundup.psfhosted.org>
In-reply-to
Content
Here is what I suggest working with sentinels would look like:


>>> from dataclasses import MISSING
>>> MISSING
dataclasses.MISSING
>>> M2 = pickle.loads(pickle.dumps(MISSING))
>>> M2
dataclasses.MISSING
>>> M2 is MISSING
True


Here's an implementation which ensures a single instance is used, even considering multi-threading and pickling, which sets a nice repr according to the module and class name:


try:
    from threading import Lock
except ImportError:
    class Lock:
        def __enter__(self):
            pass
        def __exit__(self, exc_type, exc_value, traceback):
            pass


class Sentinel:
    _instance = None
    _lock = Lock()
    def __new__(cls):
        if cls._instance is None:
            with cls._lock:
                if cls._instance is None:
                    cls._instance = super().__new__(cls)
        return cls._instance
    def __repr__(self):
        *path_parts, classname = self.__class__.__qualname__.split('.')
        return '.'.join([self.__class__.__module__, *path_parts, classname.removeprefix('_')])


class _MISSING(Sentinel):
    pass
MISSING = _MISSING()
History
Date User Action Args
2021-05-13 17:21:50taleinatsetrecipients: + taleinat, eric.smith, zach.ware, iritkatriel
2021-05-13 17:21:50taleinatsetmessageid: <1620926510.3.0.529348939246.issue44123@roundup.psfhosted.org>
2021-05-13 17:21:50taleinatlinkissue44123 messages
2021-05-13 17:21:50taleinatcreate