I have two modules:
#module_a
def make_a(cb = None):
global a
a = 1 # It takes a very long time to make "a", so it is stored as a global in module_a.
if cb != None:
cb()
And,
#module_b
import module_a
def make_b():
global b
#In order to make "b", we need module_a.a, however there is no guarantee it has been made.
if 'a' not in dir(module_a):
module_a.make_a(make_b)
return
else:
b = module_a.a + 1
make_b()
print(b) # 2
In the above code, the function make_b
in module_b
makes something named b
, which depends on something named a
in module_a
. In order to satisfy its dependency, it calls make_a
with a callback to make_b
. Once a
is made, then make_b
is called again and b
is made.
This pattern makes sense to me, but I am wondering if this is a generally accepted approach to doing this or if it is an anti-pattern.
Is there a more canonical approach to satisfying such a dependency in Python i.e., the Pythonic way?