Package: singleton-class-decorator
InstallationThrough GitHubGo to a folder where you want to store the repo:cd <path_to_folder>Clone the repo using the following:git clone git@github.com:Jrmy-rbr/singleton-class-decorator.gitInstall using pip:pip install ./singleton-class-decoratorThrough PyPIRunpython -m pip install singleton-class-decoratorSingleton Class DecoratorThis repo implements a decorator that creates singleton classes. As opposed to some other singleton decorators,
the singleton classes created through this decorator are true classes. As a consequence,
every operation supported by classes is supported by the singleton classes, like the use of instance, or inheritance for example.The main purpose of this repo was for me to learn more about Python, and some of its more advanced features. But this decorator comes with its advantages. It is not more powerful than for examplethiscommonly used decorator byKemaweyan. However, it allows using singleton classes as actual classes, and therefore it might be more intuitive to use in situations where Kemayan's singleton object needs to use some extra attribute. Here is a quick illustration of situations in which our decorator behaves differently.Quick comparison with Kemayan's decoratorKemayan's decorator@singletonclassMyClass:@staticdefadd(x,y):returnx+y# This will raise an AttributeErrorMyClass.add(1,2)# You need to do this instead. It'll return 3MyClass.__wrapped__.add(1,2)@singletonclassMyClass:...obj=MyClass()# this will raise an errorisinstance(obj,MyClass)# you need to do this insteadisinstance(obj,MyClass.__wrapped__)This decorator@singletonclassMyClass:@staticdefadd(x,y):returnx+y# This works fineMyClass.add(1,2)@singletonclassMyClass:...obj=MyClass()# this works fineisinstance(obj,MyClass)UsageYou can import the singleton class decorator as follows:fromsingleton_class_decoratorimportsingletonSimplest use caseHere are some usage examples. To make a singleton class you simply need to use the singleton decorator
on your class as follows:@singletonclassMyClass:...# MyClass is now a singleton, which we can checkassertMyClass()isMyClass()As mentioned earlier, you can check the type of an object as with a regular class:obj=MyClass()type(obj)# returns `<class 'singleton_class_decorator.MetaClasses.MyClass'>`type(obj)==MyClass# This evaluates to Trueisinstance(obj,MyClass)# This evaluates to TrueEnabling InheritanceThe above should work for any class. However, if you try to create a child class fromMyClassan error will be raised. This is because by default the singleton disables inheritance.fromsingleton_class_decoratorimportsingleton@singletonclassMyClass:...# is equivalent to the following@singleton(is_final=True)classMyClass:...As you see, the default value of the keyword argumentis_finalisTrue, which disables inheritance. But you can easily change it.# enable inheritance by setting the ketword argument `is_final` for `False`@singleton(is_final=False)classMyClass:...# This will now workclassChildClass(MyClass):...# However the ClidClass is not automatically a singletonassertChildClass()isnotChildClass()# If you want a child class to be a singleton, you need to use the singleton decorator# Again you may set the `is_final` to `True` or `False` as you whish.@singletonclassOtherChildClass(MyClass):...# OtherChildClass is a singletonassertOtherChildClass()isOtherChildClass()How Does it work?Go check my blog post about this decoratorhere