r/learnpython•
YoniF

Access values in module that imported

So I'm not quite sure if what I want is possible, or what is a good practice to achieve the same functionality

# MainModule
import B

status = "all good"

# B Module
import C

C.panic()

# C Module
def panic():
    print(status)

So the variable status is out of scope both for B and C modules. How do I access it? Or rather how would you go about it to provide the functionality the imported module having access the the module that imported it? (so C having access to A)

The only solution I see is to initially pass a reference to B which passes it on to C. But it just seems so messy. When things get bigger I could be passing 5 arguments to every function that are all references?

I tried to keep the example simple but I'll explain how I encountered this problem practically:

My main module has some "current state" variables. Module B checks some things, and decides what function needs to happen. Module C defines those functions, which many times means needing to change various "current state" variables.

Posted Oct 28, 2017, 9:42 AM UTC

11
Loading comments…