Python Singleton base class

Flora 131 Published: 12/11/2024

Python Singleton base class

The Singleton design pattern is a creational design pattern that restricts the instantiation of a class to a single instance. In Python, this can be achieved by using a private constructor and modifying the class's metaclass.

Here's an example implementation:

class Meta(type):

_instances = {}

def call(cls, *args, **kwargs):

if cls not in cls._instances:

cls._instances[cls] = super(Meta, cls).call(*args, **kwargs)

return cls._instances[cls]

class Singleton(metaclass=Meta):

def some_business_logic(self):

Restores the singleton's original state

pass

Usage

s1 = Singleton()

s2 = Singleton()

print(s1 is s2) # Output: True

In this code, Singleton is the class that we want to ensure only one instance of. The metaclass Meta defines a _instances dictionary where it stores all instances of Singleton. When we call Singleton() multiple times, the metaclass checks if an instance already exists in _instances. If not, it creates and stores the new instance.

This implementation has some limitations:

Lack of control: The Singleton pattern does not provide any control over how many instances are created or when they are garbage-collected. Global scope: This implementation stores all instances globally. For large systems with multiple Singletons, this could lead to conflicts between Singletons trying to use the same global storage.

To address these limitations, you can implement a Weakref-based Singleton:

import weakref

class Singleton(metaclass=Meta):

_instances = weakref.WeakValueDictionary()

@classmethod

def instance(cls):

if cls not in cls._instances:

cls._instances[cls] = super(Singleton, cls).call()

return cls._instances[cls]

Usage

s1 = Singleton()

s2 = Singleton()

print(s1 is s2) # Output: True

In this code, we use weakref module's WeakValueDictionary to store the instances. This allows for garbage collection of instances that are no longer referenced.

This implementation addresses some limitations:

Control: You can manually manage instance lifetime by deleting references or using del to destroy an instance. Scope: Each Singleton now has its own scope, which helps with conflicts between different Singletons.

Can we inherit abstract class in Python?

I'd be happy to help you with that!

In Python, abstract classes are defined using the ABC (Abstract Base Class) module. An abstract class is a class that cannot be instantiated on its own; instead, it provides a blueprint for other classes to follow.

Now, can we inherit from an abstract class in Python? The answer is... yes!

Let's take a step back and consider what abstract classes are all about. An abstract class typically defines one or more abstract methods (methods without implementation). These abstract methods are meant to be implemented by any concrete subclass that inherits from the abstract class.

When you define an abstract class, you use the ABC module to mark certain methods as abstract. For example:

from abc import ABC, abstractmethod

class AbstractClass(ABC):

@abstractmethod

def do_something(self):

pass

In this example, we've defined an abstract class AbstractClass with a single abstract method do_something. Any concrete subclass that inherits from AbstractClass will need to implement do_something.

Now, let's create a concrete subclass that inherits from our abstract class:

class ConcreteClass(AbstractClass):

def do_something(self):

print("I'm doing something!")

Here, we've defined a concrete class ConcreteClass that inherits from our abstract class. We've also implemented the abstract method do_something.

So, to answer your question: yes, you can absolutely inherit from an abstract class in Python! In fact, it's a great way to define a common interface for multiple concrete subclasses to follow.

Here are some benefits of using abstract classes:

Encapsulation: Abstract classes help encapsulate the implementation details of related classes, making it easier to modify or extend their behavior. Interface definition: By defining an abstract class with abstract methods, you're specifying an interface that any concrete subclass must adhere to. Code reuse: When multiple classes share a common parent class, they can inherit from that class and avoid duplicate code.

In summary, Python's ABC module makes it easy to define abstract classes and ensure their abstract methods are implemented by any concrete subclasses. This design pattern helps promote code reusability, modularity, and maintainability in your Python programs!