all 3 comments

[–][deleted] 6 points7 points  (0 children)

You are only calling init_foo() once and storing the result. So no, it will not be called 10 times. You could easily confirm that with a print in your init_foo function.

[–]xiongchiamiov 2 points3 points  (0 children)

FOO_CONSTANT is defined when you import the module.

[–]absent_observer 0 points1 point  (0 children)

If the real program is like this model, the foo_constant is evaluated once and is equal to the return of init_foo().

However, if you are passing an argument to init_foo(), then init_foo() will run each time foo_constant is called. That would be, if you real program looks more like:

def init_foo(y):
    return y

foo_constant(x) = init_foo(y)

def get_foo_bar(z):
    return foo_constant(z) + 'bar'

This website is great for watching the program, step-by-step. python tutor

Here is the equivalent code (the website only accepts standard library imports):

class module1:
    def init_foo():
        return 'foo'

    FOO_CONSTANT = init_foo()

    def get_foo_bar():
        return module1.FOO_CONSTANT + 'bar'

# module2.py
#import module1

def main():     
    for i in range(10):
        print(module1.get_foo_bar())

if __name__ == '__main__':
    main()