The singleton pattern
One of the original design patterns for OOP, the singleton pattern restricts the instantiation of a class to one object, which is useful when you need one object to coordinate actions for the system.
The basic idea is that only one instance of a particular class, doing a job, is created for the needs of the program. To ensure that this works, we need mechanisms that prevent the instantiation of the class more than once and also prevent cloning.
In the Python programmer community, the singleton pattern is actually considered an anti-pattern. Let’s explore the pattern first, and later we will discuss the alternative approaches we are encouraged to use in Python.
Real-world examples
In a real-life scenario, we can think of the captain of a ship or a boat. On the ship, they are the ones in charge. They are responsible for important decisions, and a number of requests are directed to them because of this responsibility.
Another example is the...