Singleton Design Pattern in Python

Recently, I required to create a singleton class in Python. I am putting the code for your reference.

class Singleton:
    # Here will be the instance stored.
    __instance = None
    
    @classmethod
    def get_instance(cls) -> 'Singleton':
        if cls.__instance is None:
            cls.__instance = cls()
        return cls.__instance

    @classmethod
    def clear_instance(cls) -> None:
        cls.__instance = None

    def anyofyourfunction():
        return None

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s