[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 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  hat am 31.08.2023 11:29 CEST 
> geschrieben:
>  
>  
> Seems nice. Just write a library and upload it to one of the usual places?
> 
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/4L7EU26NWJGUZPZ3W5WXRGQJLKNRSLDH/
Code of Conduct: http://python.org/psf/codeofconduct/


[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 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}
>   >>> 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)
> 
> >>> 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-le...@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/BXTIVPVDHW644AX3EAJU36A2GFCPX3QS/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/BMM3ARGWC3AJGPHNUP5IUHYEGHK57RIC/
Code of Conduct: http://python.org/psf/codeofconduct/


[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://peps.python.org/pep-0661/

There are a few options here, including classes, enums, and a
dedicated sentinel type.

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/6C2FSMYQ2PBRUXRU7MVXTN2J6OJH64NZ/
Code of Conduct: http://python.org/psf/codeofconduct/


[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}
  >>> 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)

>>> 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-le...@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/BXTIVPVDHW644AX3EAJU36A2GFCPX3QS/
Code of Conduct: http://python.org/psf/codeofconduct/


[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.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: 
>  
> - 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)
> 
> >>> 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-le...@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/UJ7JOMBIH64SBP6LHXFPNUCOJSSZHE47/
Code of Conduct: http://python.org/psf/codeofconduct/