
On 20 May 2021, at 19:10, Luciano Ramalho <luciano@ramalho.org> wrote:
I'd like to learn about use cases where `...` (a.k.a. `Ellipsis`) is not a good sentinel. It's a pickable singleton testable with `is`, readily available, and extremely unlikely to appear in a data stream. Its repr is "Ellipsis".
If you don't like the name for this purpose, you can always define a constant (that won't fix the `repr`, obviously, but helps with source code readability).
SENTINEL = ...
I can't think of any case where I'd rather have my own custom sentinel, or need a special API for sentinels. Probably my fault, of course. Please enlighten me!
One use case for a sentinel that is not a predefined (builtin) singleton is APIs where an arbitrary user specified value can be used. One example of this is the definition of dataclasses.field: dataclasses.field(*, default=MISSING, default_factory=MISSING, repr=True, hash=None, init=True, compare=True, metadata=None) Here the “default” and “default_factory” can be an arbitrary value, and any builtin singleton could be used. Hence the use of a custom module-private sentinel that cannot clash with values used by users of the module (unless those users poke at private details of the module, but then all bets are off anyway). That’s why I don’t particularly like the proposal of using Ellipsis as the sanctioned sentinel value. It would be weird at best that the default for a dataclass field can be any value, except for the builtin Ellipsis value. Ronald
Cheers,
Luciano
On Thu, May 20, 2021 at 8:35 AM Victor Stinner <vstinner@python.org> wrote:
IMO you should consider writing a PEP to enhance sentinels in Python, and maybe even provide a public API for sentinels in general.
Victor _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@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/EY6B6PRQ... Code of Conduct: http://python.org/psf/codeofconduct/
-- Luciano Ramalho | Author of Fluent Python (O'Reilly, 2015) | http://shop.oreilly.com/product/0636920032519.do | Technical Principal at ThoughtWorks | Twitter: @ramalhoorg _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@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/WNCFFIJH... Code of Conduct: http://python.org/psf/codeofconduct/
— Twitter / micro.blog: @ronaldoussoren Blog: https://blog.ronaldoussoren.net/