suss-swe-ig / suss-telegram-groups

Bot for sharing SUSS Telegram Groups
MIT License
0 stars 0 forks source link

Implement Singleton as a metaclass instead of a base class. #17

Open kaixiong opened 1 month ago

kaixiong commented 1 month ago

Per the description, this patch correctly implements Singleton as a metaclass.

This works by re-implementing the object's metaclass __call__ method such that its class __new__ (and __init__) is called only once. Subsequent object creation returns the prior constructed instance:

class Example(metaclass=Singleton):
    pass

a = Example()
b = Example()
assert a is b