I am trying to register classes that are in different files to the factory class. The factory class has a dictionary called "registry" which hold/maps the a user defined name to the registering class. My issue is that if my factory class and registering classes are in the same .py file everything works as expected but the moment I move the registering classes into their own .py files and import the factory class to apply the register decorator (as described in the question & article below) the "registry" dictionary stays empty, which means that the classes are not getting registered.
They way I am registering these classes is via a decorator. My code looks very much like what we see here:
- Registering classes to factory with classes in different files (my question is a duplicate of this, but bumping this question to the top)
- https://medium.com/@geoffreykoh/implementing-the-factory-pattern-via-dynamic-registry-and-python-decorators-479fc1537bbe
I would like to know:
- What why keeping them in the same file work while splitting them out doest
- How can I make the separate file approach work ?
Hopefully the code samples in the articles clarify what I am trying to do and struggling with.