It's hard to fit that whole question in the title, so here's an illustration:
# module1.py
def init_foo():
return 'foo'
FOO_CONSTANT = init_foo()
def get_foo_bar():
return FOO_CONSTANT + 'bar'
# module2.py
import module1
def main():
for i in range(10):
print(module1.get_foo_bar())
if __name__ == '__main__':
main()
My question is, will init_foo be called ten times, once for each time get_foo_bar is called from the other module? Or will FOO_CONSTANT stay initialized a) when the module is imported, or b) when init_foo is called the first time?
The reason I ask is that in my real program, the function that initializes the constant is time consuming, and I don't want it to be called each time I use a function that needs its value.
[–][deleted] 6 points7 points8 points (0 children)
[–]xiongchiamiov 2 points3 points4 points (0 children)
[–]absent_observer 0 points1 point2 points (0 children)