zhangjun / my_notes

0 stars 0 forks source link

Python #17

Open zhangjun opened 10 months ago

zhangjun commented 10 months ago

装饰器

@wrapt @functools.wrap functools.partial

def singleton(cls):
    instances = {}

    def wrapper(*args, **kwargs):
        if cls not in instances:
            instances[cls] = cls(*args, **kwargs)
        return instances[cls]

    return wrapper

@singleton
class Logger:
    def __init__(self):
        self.log_file = open("log.txt", "a")

    def log(self, message):
        self.log_file.write(f"{message}\n")
rushi-02 commented 9 months ago

After reviewing the code above ,I think you're attempting to receive logs of all instances of same class into a single log.txt file. Please elaborate more on what's the issue you are facing.