What is the most appropriate way to pass objects between modules in python?
Question: I have to access an object in a module from another module:

module_1.py:

    
    
    import module_2
    
    class A():
        def __init__():
             do_sth()
    
    class B():
        def __init__():
             do_sth()
             self.x = some_object()
             do_sth_else(x)
    
        def show():
            show_gui()
    
    def start():
        y = B()
        y.show()
    
    if __name__ == "__main__":
        start()
    

module_2.py:

    
    
    def run_it(arg):
        run_run_run()
    

I need `self.x` object from `module_1` so that I can pass it as an argument to
`run_it()` function in `module_2`. Note that `module_1` also imports
`module_2`.

Is there a conventional method to access objects in other modules?


Answer: Now with your corrections, it looks like you want something like this:

    
    
    from module_1 import B
    
    run_it(B().x)
    

Which raises a question: If `x` is the same for all objects of `B`, why not
make it a class member instead of an instance member. Something like:

    
    
    class B:
        x = some_object()
    
        def __init__(self):
             do_sth()
             do_sth_else(B.x)
    
        def show(self):
            show_gui()
    

And in the other module

    
    
    run_it(B.x)
    

* * *

Addressing your comment: `y` is local to `start`. You will need to return it:

    
    
    def start():
        y = B()
        y.show()
        return y
    

And then

    
    
    run_it(start())
    

* * *

Also `y.show()` will fail horribly as `show` doesn't accept parameters and
will get passed `y` as `self`.

