On 12/05/2026 00:35, Chris Angelico wrote:
On Tue, 12 May 2026 at 09:06, Rob Cliffe via Python-list
<[email protected]> wrote:
I've read PEP 661 and I have a question - can anybody answer it?
The PEP makes clear that any call to sentinel() will return a fresh
sentinel object - even if it is called twice with the same string parameter.
It states that sentinel objects have two public attributes: __name__ and
__module__.
It also states that pickling and unpickling a sentinel will return the
same object:
MISSING = sentinel('MISSING')
assert pickle.loads(pickle.dumps(MISSING)) is MISSING

So if I write (in the same module, in the global scope):

somename = 'MISSING'
MISSING1 = sentinel(somename)
MISSING2 = sentinel(somename)
assert MISSING2 is not MISSING1
LAZARUS1 = pickle.loads(pickle.dumps(MISSING1))
LAZARUS2 = pickle.loads(pickle.dumps(MISSING2))

how does Python know that
      LAZARUS1 is MISSING1
      LAZARUS2 is MISSING2 is not MISSING1
Try it and you'll see! You won't get that far; it'll raise an exception.

ChrisA
Do I need to install Python 3.15 to try it?
I did try it (before posting) with Python 3.13, using the outline implementation from the PEP; it allowed me to create 2 sentinels but  failed when I tried pickle.dumps on the first one with _pickle.PicklingError: Can't pickle MISSING: attribute lookup MISSING on __main__ failed
Even when I just create one sentinel:

MISSING1 = sentinel("MISSING")
D1 = pickle.dumps(MISSING1)

I get the same error.
Maybe it would be different with Python 3.15.
Are you telling me that trying to create 2 sentinels with an identical string would raise an Exception in 3.15?
Thanks
Rob Cliffe

--
https://mail.python.org/mailman3//lists/python-list.python.org

Reply via email to