
The standard pattern to create a sentinel in Python is
Unset = object()
While this is often good enough, it has some shortcomings: - repr(Unset) is unhelpful: <object at 0x1ded9911b60> - copy/deepcopy create a copy of the sentinel object, which can lead to surprising results such as:
- The code "Unset = object()" is quite obscure for people not familiar with the sentinel pattern. I propose to provide a sentinel() factory function so that
A simple implementation would be something like def sentinel(name): return type(name, (), { '__repr__': lambda self: f"<{self.__class__.__name__}>", '__copy__': lambda self: self, '__deepcopy__': lambda self, memo: self, }) Likely, the implementation should be refined a bit more. While it's not rocket science, getting sentinels correct is cumbersome for end users. Providing such a function in the standard library is only a minor maintainance burden, but a significant help for users working with sentinels. Thanks for your consideration! Tim

On Thu, 31 Aug 2023 at 18:55, Tim Hoffmann via Python-ideas <python-ideas@python.org> wrote:
Looks like you may be thinking of this: https://peps.python.org/pep-0661/ There are a few options here, including classes, enums, and a dedicated sentinel type. ChrisA

There is already https://pypi.org/project/sentinel/ https://pypi.org/project/sentinels/ Though, I think this should become part of the standard library. It's a fundamental concept, somewhat analogous to namedtuples, enums and dataclass (only a bit less used, but also less complex). Once figured out, the implementation will be very stable. I believe it's not good to depend for such small and stable functionality on third party packages. At least, I'd often then just stay with the sentinel = object() approach. I think https://peps.python.org/pep-0661/ is going very much in the right direction. Will continue discussing in this context.
Matthias Görgens <matthias.goergens@gmail.com> hat am 31.08.2023 11:29 CEST geschrieben:
Seems nice. Just write a library and upload it to one of the usual places?

On Thu, 31 Aug 2023 at 18:55, Tim Hoffmann via Python-ideas <python-ideas@python.org> wrote:
Looks like you may be thinking of this: https://peps.python.org/pep-0661/ There are a few options here, including classes, enums, and a dedicated sentinel type. ChrisA

There is already https://pypi.org/project/sentinel/ https://pypi.org/project/sentinels/ Though, I think this should become part of the standard library. It's a fundamental concept, somewhat analogous to namedtuples, enums and dataclass (only a bit less used, but also less complex). Once figured out, the implementation will be very stable. I believe it's not good to depend for such small and stable functionality on third party packages. At least, I'd often then just stay with the sentinel = object() approach. I think https://peps.python.org/pep-0661/ is going very much in the right direction. Will continue discussing in this context.
Matthias Görgens <matthias.goergens@gmail.com> hat am 31.08.2023 11:29 CEST geschrieben:
Seems nice. Just write a library and upload it to one of the usual places?
participants (3)
-
Chris Angelico
-
Matthias Görgens
-
Tim Hoffmann