Skip to content

Singleton decorator that preserves the decorated class

Notifications You must be signed in to change notification settings

kremnik/singleton-decorator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Singleton Pattern Implementation

MetaSingleton Metaclass

The MetaSingleton metaclass ensures that only one instance of any class using this metaclass is created. It maintains a dictionary of instances, where each key is the class representation (either repr(cls) if defined, or a combination of module and class name) and each value is the single instance of that class.

Singleton Class

The Singleton class uses MetaSingleton as its metaclass. Any class inheriting from Singleton will automatically follow the singleton behavior.

singleton Decorator Class

The singleton class is a decorator class. Its task is to add the Singleton class to the list of base classes of the decorated class. As a result, because the Singleton's metaclass is MetaSingleton, the decorated class inherits this metaclass and becomes a singleton.

Be careful with ABC and ABCMeta

Do not use this decorator if you are using ABC class or ABCMeta metaclass, as a "singletoned" class can only be created if the metaclass of this class is type.

Usage

from singleton import singleton

@singleton
class RegularClass:
    def __init__(self):
        print("Initializing RegularClass")

    @classmethod
    def classname(cls):
        return cls.__name__

# The "singletoned" class is the original class (not a Singleton class, not a function, etc.)
print(RegularClass.classname() == "RegularClass") # Outputs: True

obj1 = RegularClass()
obj2 = RegularClass()

print(obj1 is obj2) # Outputs: True

# Check if class or instance is a singleton
print(hasattr(RegularClass, "is_singleton")) # Outputs: True
print(hasattr(obj1, "is_singleton")) # Outputs: True

About

Singleton decorator that preserves the decorated class

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages