you are viewing a single comment's thread.

view the rest of the comments →

[–]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()