Python: Singleton (2)

June 28, 2017


Using the singleton technique introduced previously, it is easy for us to target a problem where only a limited number of instances are to be instantiated. For example, consider the following problem: create a class that manages a fixed number of its own objects. Assume the objects are database connections and you only have a license to use a fixed quantity of these at any one time.


I am showing my codes for the classes as below. In this example, I assume only 3 licenses are available; if any additional user wants to use the license, a previous user will get kicked out.

The results look like the following:

Note that at any given time, there will be a maximum number of 3 licenses.