
Thanks for this, Luciano! My main issue with using class objects is that such use of them would be unusual and potentially confusing. I think that is important in general, and doubly so for something I suggest adding to the stdlib. Additionally, I very much would like for each sentinel object to have its own dedicated class, to allow writing strict function type signatures. Once one adds that to a meta-class or class decorator, the complexity of the implementation approaches what I currently have for a sentinel() function. At that point, I can't see a clear advantage for such an approach. - Tal On Mon, May 24, 2021 at 7:31 PM Luciano Ramalho <luciano@ramalho.org> wrote:
But frankly Luciano's idea of a base class that can be subclassed seems the most startightford to me.
Yes, and it's what I originally suggested near the beginning of this
On Mon, May 24, 2021 at 11:44 AM Tal Einat <taleinat@gmail.com> wrote: thread :)
I am sorry to have missed your previous e-mail with a base class for sentinels, @Tal. I support that idea.
In fact, if we have a SentinelMeta metaclass, and a Sentinel base class built from SentinelMeta, the Sentinel class can be used directly as a sentinel without the need for subclassing—if the application does not require a custom sentinel. If it does, then the user can subclass Sentinel.
The SentinelMeta could be private, to discourage misuse.
This is my implementation, after learning from @Tal's code:
https://github.com/fluentpython/example-code-2e/blob/master/25-class-metapro...
Since having a Sentinel base class is desirable for ease of use, I think it is simpler to code the __new__ method in it, instead of coding metaclass logic to inject __new__ in the class namespace.
Best,
Luciano
-- Luciano Ramalho | Author of Fluent Python (O'Reilly, 2015) | http://shop.oreilly.com/product/0636920032519.do | Technical Principal at ThoughtWorks | Twitter: @ramalhoorg