On Fri, May 21, 2021 at 3:51 AM David Mertz <me...@gnosis.cx> wrote: > > On Thu, May 20, 2021 at 6:21 AM Tal Einat <talei...@gmail.com> wrote: >> >> On Sat, May 15, 2021 at 2:09 AM David Mertz <me...@gnosis.cx> wrote: >> > Just add a ._uuid attribute and have object equality follow equality of >> > that attribute. There's no reason to expose that in the .__repr__, but it >> > would be inspectable in concept. >> >> I think it's worth preserving the idiom of comparing sentinels using >> `is`, as we do for `None` and other existing sentinel values. It's >> relatively easy to do, such as by using a single-value Enum or by >> using a class with a custom __new__. > > > This only works if: > > a) Unpickling is within a single interpreter session > b) Sentinels are explicitly created in imported modules, not as a runtime, > user-level creation > > Maybe there's a way to do it, but how would you handle this situation: > > if some_runtime_condition: > my_sentinal = Sentinel(desc="Gosh, I need a sentinel") > # ... code ... > pickle.dump(thing_using_sentinel, fh) > > Equality is certainly a lot easier to get than identity here. >
Probably the easiest way would be to have some kind of unique identifier (a fully-qualified name) that can be pickled, and then any time you attempt to construct a Sentinel with that identifier, it's guaranteed to return the same object. That way, in your example, unpickling it in another session where that runtime condition is false would simply create a brand new sentinel, which would be the same as any others that also get unpickled; and if, subsequently, you reconstruct my_sentinal, it would be the same one as got unpickled. The hardest part would be figuring out a reliable way to define the identifier, without massively duplicating or requiring that something be registered somewhere. ChrisA _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/3WKMD7Q3JMLDWI3L2WOGIJB767PYJO2W/ Code of Conduct: http://python.org/psf/codeofconduct/