Answers for "singleton class python logger"

0

singleton class python logger

# What the Gang of Four’s original Singleton Pattern
# might look like in Python.

class Logger(object):
    _instance = None

    def __init__(self):
        raise RuntimeError('Call instance() instead')

    @classmethod
    def instance(cls):
        if cls._instance is None:
            print('Creating new instance')
            cls._instance = cls.__new__(cls)
            # Put any initialization here.
        return cls._instance
Posted by: Guest on April-11-2021

Python Answers by Framework

Browse Popular Code Answers by Language