[Python-ideas] Re: factory for sentinel objects

2023-08-31 Thread Tim Hoffmann via Python-ideas
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 hat am 31

[Python-ideas] factory for sentinel objects

2023-08-31 Thread Tim Hoffmann via Python-ideas
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: - copy/deepcopy create a copy of the sentinel object, which can lead to surprising results such as: >>> d = {'val': Unset}

[Python-ideas] Re: factory for sentinel objects

2023-08-31 Thread Chris Angelico
On Thu, 31 Aug 2023 at 18:55, Tim Hoffmann via Python-ideas 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: > Looks like you may be thinking of this: https:/

[Python-ideas] Re: factory for sentinel objects

2023-08-31 Thread Matthias Görgens
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

[Python-ideas] Re: factory for sentinel objects

2023-08-31 Thread Tim Hoffmann via Python-ideas
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 figur