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:
d = {'val': Unset} d['val'] is Unset True d2 = copy.deepcopy(d) d2['val'] is Unset False
- 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
Unset = sentinel("Unset") repr(unset) <Unset> copy.copy(Unset) is Unset True copy.deepcopy(Unset) is Unset True
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
Sorry, minor bug in the example implementation: def sentinel(name): cls = type(name, (), { '__repr__': lambda self: f"<{self.__class__.__name__}>", '__copy__': lambda self: self, '__deepcopy__': lambda self, memo: self, }) return cls()
Tim Hoffmann <tim.hoffmann@mailbox.org> hat am 31.08.2023 10:44 CEST geschrieben:
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:
d = {'val': Unset} d['val'] is Unset True d2 = copy.deepcopy(d) d2['val'] is Unset False
- 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
Unset = sentinel("Unset") repr(unset) <Unset> copy.copy(Unset) is Unset True copy.deepcopy(Unset) is Unset True
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:
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>
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
Seems nice. Just write a library and upload it to one of the usual places? On Thu, 31 Aug 2023, 16:54 Tim Hoffmann via Python-ideas, < python-ideas@python.org> wrote:
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:
d = {'val': Unset} d['val'] is Unset True d2 = copy.deepcopy(d) d2['val'] is Unset False
- 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
Unset = sentinel("Unset") repr(unset) <Unset> copy.copy(Unset) is Unset True copy.deepcopy(Unset) is Unset True
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 _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/BXTIVP... Code of Conduct: http://python.org/psf/codeofconduct/
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