How to run a module in Python?

1

I have a question. A few days ago I'm creating an application with a GUI interface to show movie schedules. It was built in modules, for example a module takes care of the images, searching the web and downloading. Another module takes care of other information like searching for synopses, budgets of the films, actors. Another, takes care of picking up schedules and information from the cinema and another from the GUI.

My question is, how to interconnect these modules? Because not everything in the module is within function. The imaging module lowers the movie covers by means of a for outside a function.

How do I make my main module run all module code? Should I make everything turn function?

This project is being done just to help me learn. Thanks in advance!)

    
asked by anonymous 31.07.2015 / 18:38

1 answer

1

When you import a module, if you have module-level code (such as for loop), it will be executed at the same time. So if you want some code to be executed right away at the moment of import , you can either make this code global (like your for loop), or you can encapsulate it into a function, and call it directly from your module or the module where it is defined.

For example, suppose I have the following module A :

# módulo A

# código global
for i in range(10):
    print(i)

# função qualquer
def some_function():
    print("some function")

What you want to import into the following module B :

# módulo B

import A  # importando módulo A

A.some_function()

Just when you import A , the for loop will execute immediately because it is not encapsulated in a function. Note well that the some_function function code runs because I'm calling it.

    
03.08.2015 / 17:04