[Python] The singleton class method is not being invoked again. Python

Discussão em 'Python' iniciado por Stack, Maio 19, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a project that involves searching for words in files (https://github.com/BogomolML/searchEngine). After writing all the necessary code, I encountered a problem with reindexing. After debugging, I realized that my init and build_index functions were not being called. I suspect this is related to a singleton issue, but I'm not sure how to fix it.

    I tried changing the singleton and creating a new object:

    def singleton(cls):
    instances = {}

    def wrapper(*args, **kwargs):
    force_new = kwargs.pop('force_new', False)
    if force_new or cls not in instances:
    instances[cls] = cls(*args, **kwargs)
    return instances[cls]
    return wrapper


    I also tried writing a rebuild method, where I called the same methods from within the class.

    When I try to create a minimal reproducible example, everything seems to work fine for me, so I am not sure where the problem might be. I am using a Singleton pattern so that when I create objects in different parts of my program, I can still access a shared dictionary.

    Continue reading...

Compartilhe esta Página