On Mon, May 24, 2021 at 11:44 AM Tal Einat <taleinat@gmail.com> 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 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-metaprog/sentinel/sentinel.py
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