Coverage for /home/ubuntu/lunchbox/python/lunchbox/singleton.py: 100%

5 statements  

« prev     ^ index     » next       coverage.py v7.9.0, created at 2025-06-13 03:03 +0000

1class Singleton: 

2 ''' 

3 A super class for creating singleton classes. 

4 ''' 

5 def __new__(cls, *args, **kwargs): 

6 ''' 

7 __new__ is called before __init__. Normaly __new__ fetches an object and 

8 __init__ initilaizes it. 

9 

10 In this class, __new__ checks the class body for an instance of a class, 

11 returns it if it already exists, and creates, assigns and returns it if 

12 it does not. 

13 

14 Returns: 

15 object: Singular instance of class. 

16 ''' 

17 if not hasattr(cls, '_instance'): 

18 cls._instance = super(Singleton, cls).__new__(cls) 

19 return cls._instance