Using __new__

Mats Wichmann mats at wichmann.us
Sun Feb 18 10:52:12 EST 2024


On 2/17/24 19:24, dn via Python-list wrote:
> On 18/02/24 13:21, Jonathan Gossage wrote:

> - perhaps someone knows a better/proper way to do this?
> 
> Suggested research: custom classes, ABCs, and meta-classes...

Cure the old "what do you want to accomplish" question.  If it's to 
channel access to a resource to a single place, many folks seem to 
advocate just putting that code in a module, and not trying to use a 
class for that - Python already treats modules as a form of singleton 
(if you squint a bit). It's not Java, after all, everything doesn't 
_have_ to be a class.

I'd also second the idea of looking at metaclasses for an 
implementation. Most simpler class-based singleton approaches turn out 
not to be thread-safe... you can get closer to solving that with a 
metaclass with a lock taken in the dunder-call method.




More information about the Python-list mailing list