The article discusses the implementation of the Singleton pattern in Python, its history, and alternatives to using Singleton classes.
Abstract
The Singleton pattern is a design pattern that ensures a class has only one instance and provides a global point of access to it. Although controversial, it is useful for certain scenarios. The article explains the history of the Singleton pattern, which was introduced in the book "Design Patterns: Elements of Reusable Object-Oriented Software" in 1994. It then discusses how to implement the Singleton pattern in Python using the __new__() method. The article also mentions that Python's multi-paradigm nature allows for alternatives to Singleton classes, such as using modules to share global variables.
Bullet points
The Singleton pattern is a design pattern that ensures a class has only one instance and provides a global point of access to it.
The Singleton pattern was introduced in the book "Design Patterns: Elements of Reusable Object-Oriented Software" in 1994.
In Java, the Singleton pattern is implemented using a private and static variable to represent the only instance, making the class constructor private, and using a static getInstance() method to access the instance.
In Python, the Singleton pattern can be implemented using the __new__() method.
Python's multi-paradigm nature allows for alternatives to Singleton classes, such as using modules to share global variables.
The Django web development framework uses a Python file called settings.py to store configuration-related global variables.
3 Levels of Understanding the Singleton Pattern in Python
The singleton is a simple but controversial design pattern. For some classes that only one instance of them is needed, such as logging, settings, caches or device drivers related objects, instantiating more than one instances could introduce many unexpected bugs. Therefore, the idea of singleton, which is restricting the special classes’ abilities to only generate at most one instance, is useful for some scenarios.
On the other side, the singleton pattern is considered an anti-pattern by some developers. Because it will introduce unnecessary restrictions and global variables. Not to mention it reduces the readability of code.
Although controversial, it is often used and discussed when it comes to object-oriented programming. This article will introduce the history and concepts of the singleton pattern from the respective of Python.
1. Know the History of the Singleton Design Pattern
The singleton pattern was first introduced by the famous book “Design Patterns: Elements of Reusable Object-Oriented Software” in 1994:
Singleton: Ensure a class only has one instance, and provide a global point of access to it.
Ten years later, another important book in the history of computer science published, “Head First Design Pattern”. This book uses Java, a nature object-oriented language, to introduce and apply all the famous design patterns. To some extent, this book is even one of the key factors in the popularity of Java.
Therefore, before we think about Python, it’s worth to know how Java implements the singleton pattern. A simple and classic method is as follows:
As shown above, the key ideas are:
Declare a private and static variable to represent the only instance.
Make the class constructor be a private method, so we can’t instantiate a new instance outside the class itself.
The static getInstance() method is the way that the outside code accesses the one instance (instantiate one if it’s not existed).
The above implementation can still be improved to make it more reliable, such as using the enum keyword in Java. But the idea is similar and we aren’t to talk about Java too much.
Unfortunately, Python doesn’t have access modifiers like public or private. Does it mean that we can’t control the constructors of Python classes?
In fact, Python gives us more flexibility to modify the constructors of classes. We can implement the singleton pattern more easily in Python. The key is the __new__() method. A common template to implement it is as follows:
The __new__() method is a special method (or called magical method) in Python classes. It’s a default constructor to create and return a new instance of the class. In most cases, we don’t need to modify this default method. But when it comes to the singleton pattern, we can change it a bit to ensure that only one instance of the class will be created. Based on the similar idea of Java, we define a static and private variable to represent the one instance. In Python, as the template code shows, we use the double leading underscores to define a variable as a private member.
Let’s see an example:
As the above example shows, the s1 and s2 are the same instances. Because the class Singleton_Genius are restricted to be a singleton.
By the way, this is just one of the feasible implementations of the singleton pattern in Python. There are many other ways, from decorators to metaclasses.
3. Avoid the Singleton Classes in Python
As a strictly object-oriented programming language, everything in Java must be a class. However, Python is a multi-paradigm programming language. We don’t have to make everything to be classes. A singleton is not necessarily to be a class as well.
It mentions that using a module is also the way to implement the singleton design pattern in Python.
Because there is only one instance of each module, any changes made to the module object get reflected everywhere.
The Django web development framework applies this idea. Every new Django project will automatically generate a Python file called settings.py which includes configurations related global variables, such as database settings.
Conclusion
In Python, the singleton pattern can be applied by many ways, even without a class. We can choose different implementations depending on our specific using scenarios.
After all, design patterns are not rules that we must follow all the time. They are good programming conventions and philosophies based on experience. For different programming language, we can apply them by different ways.
Thanks for reading. If you like it, don’t forget to follow me to get more great articles about programming and technologies!