Python Concepts/Modules

Objective

 * To become familiar with some of the more common modules.


 * To understand the syntax associated with modules.


 * To experiment with what you can do and what you can't do with modules.


 * To appreciate the extra power and flexibility which modules provide.

Page Index

 * Lesson
 * Assignments
 * Further Reading or Review
 * References

Lesson
As powerful as Python is, it would be almost impossible to write meaningful code without importing certain modules.

Yes, handwritten code can sometimes replace information available in a module. For example, your code might contain the statement: and your hand-written code is as good as: In this situation you might decide that it's not necessary to

But, what about a statement like: This statement provides essential information about the most recent error which execution of your code has caused. Only an expert could create hand-written code to replace this statement and an expert would not want to do so.

When so many powerful features are available in python's modules, it makes good sense to use them. With appropriate modules imported, your code reflects your good knowledge of python and your willingness to reap the benefits of the labor of others whose contribution to python makes our professional lives easier.

You're already familiar with some of python's modules:

In this lesson we'll look first at module  and the basic syntax concerning the use of module

Importing Module string
The simplest way to use module string is to open python and import it. To see what you have imported: This shows that module  has been imported and it's available, but it doesn't tell us much about string.

For more information about module string: Now that you know what's in module string, you can access the contents of string by name:

Importing Contents of Module string
For more information about the contents of string, change syntax slightly and try again: With a little formatting we see: The values in the left hand column above are keys of dictionary globals and they are always strings. In the right hand column are the values associated with the keys of the left hand. As you can see, the values may or may not be strings.

Because globals is a dictionary, it may be accessed like a dictionary: The one name not seen in globals is string. The contents of string are now available: Experiment with the contents of string:

Note:

While the statement  is convenient, Python's Documentation states:

In most cases Python programmers do not use this facility since it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined.

.... in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.

Using module string
After you become familiar with the contents of module string, you may decide to import only the module and use the syntax that requires the name of the module: You can change the contents of module string, but this not recommended. In extreme cases, you may have to quit python and start again.

Creating your own module
In this case we will be working with module  If this module is new to you, spend some time looking at module   as if it were module   above.

In the present working directory create a Python source file, for example,  This will become module Invoke Python in interactive mode:

Slightly more advanced
Let your file  be: Invoke Python in interactive mode:

globals unique to each module
This example (on UNIX) shows how to access global variables across modules.

Create 2 files  and

def test1_ReceiveGlobals(dict1) : for v in dict1 : globals[v] = dict1[v]
 * 1) test1.py

def test1_SendGlobals : return globals

from test1 import *
 * 1) test.py

def set_globals : t1 = (   ('test1_23',67),    ('test1_24',83),    ('common_1',17), # This name is repeated below against "global".    ('common_2',23), # This name is repeated below against "global".    ) dict1 = dict( t1  ) test1_ReceiveGlobals(dict1) global test_46,test_47 global common_1,common_2                  # These 2 names exist in "dict1" above. test_46 = 79 test_47 = 41 common_1 = 'φιλοσοφοῦμεν ἄνευ μαλακίας.'  # This name exists in "dict1" above. common_2 = 'These are the times that ....' # This name exists in "dict1" above.

def print_globals : print print ('globals:') for v in [v for v in globals if '__' not in v]: print ('globals[{}] = {}'.format(v, globals[v]))

test1_globals = test1_SendGlobals print print ('test1_globals:') for v in [v for v in test1_globals if '__' not in v]: print ('test1_globals[{}] = {}'.format(v, test1_globals[v]))

print_globals set_globals print_globals

Invoke test.py: $ python3.6 ./test.py  >sout  2>serr ; echo $? ; wc sout serr 0     28     107    1224 sout 0      0       0 serr 28    107    1224 total


 * 1) file sout

globals: globals[test1_ReceiveGlobals] =  # Common to both. globals[test1_SendGlobals] =       # Common to both. globals[set_globals] =     # Unique to test. globals[print_globals] =  # Unique to test.

test1_globals: test1_globals[test1_ReceiveGlobals] =  # Common to both. test1_globals[test1_SendGlobals] =       # Common to both.

globals: globals[test1_ReceiveGlobals] =  # Common to both. globals[test1_SendGlobals] =       # Common to both. globals[set_globals] =     # Unique to test. globals[print_globals] =  # Unique to test. globals[test_46] = 79                                           # Unique to test. globals[test_47] = 41                                           # Unique to test. globals[common_1] = φιλοσοφοῦμεν ἄνευ μαλακίας. # Name exists in test1 below. Value is different. globals[common_2] = These are the times that .... # Name exists in test1 below. Value is different.

test1_globals: test1_globals[test1_ReceiveGlobals] =  # Common to both. test1_globals[test1_SendGlobals] =       # Common to both. test1_globals[test1_23] = 67 # Unique to test1. test1_globals[test1_24] = 83 # Unique to test1. test1_globals[common_1] = 17 # Name exists in test above. Value is different. test1_globals[common_2] = 23 # Name exists in test above. Value is different.

Imported module invokes function in main module
File

File

Invoke main5.py:

$ python3.6 ./main5.py Traceback (most recent call last): File "./main5.py", line 6, in    test5.test5_doit File "...../test5.py", line 2, in test5_doit main5_doit NameError: name 'main5_doit' is not defined $

Add one line of code to file

Invoke main5.py:

$ python3.6 ./main5.py in main5_doit $

Function  in main module was invoked by code in imported module

Assignments


In the output of  are two names   and   Because this page is about modules generally, and module  is used as an example, there has been no information presented concerning these two classes. For more information about these classes, see "Custom String Formatting" and "Template strings."

Experiment with statements such as,  ,  , .

Become familiar with the syntax associated with contents of modules, such as: ,,  ,  ,

Experiment with files. In  include statements. Implement functionality so that code in  can invoke functions in   and , and vice-versa.

Further Reading or Review

 * Previous Lesson: Built-in Functions
 * This Lesson: Modules
 * Next Lesson: Classes
 * Course Home Page