Modules
Modules are a very convenient way to organize reusable bits of code. Modules are imported using the “import” directive and are loaded into their own namespace. We can import the functions that we just defined in the last example and use them. It’s also possible to import some or all functions from a module into the current namespace. Here, we reuse the functions that we created in the previous example. Make sure the file containing the functions is saved as “functions.py”, otherwise python will not find the module. Also, note that any code in the main block of a module is executed when the module is imported.
[code lang=”python”]
#modules
import functions
a = [”boutros”, “boutros”, “gahli”]
print functions.selRand(a)
from functions import crit
b = [”armalite”, “kalashnikov”, “colt”, “walther”, “heckler and koch”, “steyr”]
c = [”shy”, “capricous”, “hyperactive”, “charming”, “precocious”, “sweet”]
crit(a,b,c)
[/code]