singleton class python logger

Solutions on MaxInterview for singleton class python logger by the best coders in the world

showing results for - "singleton class python logger"
Simon
04 May 2020
1# What the Gang of Four’s original Singleton Pattern
2# might look like in Python.
3
4class Logger(object):
5    _instance = None
6
7    def __init__(self):
8        raise RuntimeError('Call instance() instead')
9
10    @classmethod
11    def instance(cls):
12        if cls._instance is None:
13            print('Creating new instance')
14            cls._instance = cls.__new__(cls)
15            # Put any initialization here.
16        return cls._instance
17