I am writing a small application (several KLOCs) of PyQt. I started writing it in nice modules for ease of understanding, but I rely on Python's namespace rules. At several points, it is important to create an instance of only one class object as a resource for other code.
For example: an object that represents Aspell, attached as a subprocess, offering a verification method (words). Another example: an application has one QTextEdit and another code that should call the methods of this singular object, for example. "ifEditWidget.document (). isEmpty () ..."
No matter where I create an instance of such an object, it can only be referenced in the code in this module, and the other is not. So, for example, the code of the editing widget cannot call the Aspell gateway object if the Aspell object is not created in the same module. Good, except that it is necessary for other modules.
The bunch class is proposed for this question , but it seems that I have the same problem: it is a unique object that can only be used in the module where it was created. Or am I generally missing a boat here?
OK suggested elsewhere, this seems like a simple answer to my problem. I just tested the following:
junk_main.py:
import junk_A singularResource = junk_A.thing() import junk_B junk_B.handle = singularResource print junk_B.look()
junk_A.py:
class thing(): def __init__(self): self.member = 99
junk_B.py:
def look(): return handle.member
When I run junk_main, it prints 99. Thus, the main code can enter names into modules only for their intended purpose. I'm trying to come up with reasons, this is a bad idea.