Message393590
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() |
|
Date |
User |
Action |
Args |
2021-05-13 17:21:50 | taleinat | set | recipients:
+ taleinat, eric.smith, zach.ware, iritkatriel |
2021-05-13 17:21:50 | taleinat | set | messageid: <1620926510.3.0.529348939246.issue44123@roundup.psfhosted.org> |
2021-05-13 17:21:50 | taleinat | link | issue44123 messages |
2021-05-13 17:21:50 | taleinat | create | |
|