|
Creating ModulesDefining Functions Table Of Contents List Basics Page Contents (hide) As you learned earlier, Python includes a standard library containing modules of functions and class definitions which we have used in a number of our programs. By using modules, the language itself can remain relatively small while still providing extended functionallity. Python programs can quickly become very large and unmanagable if the entire user code is placed within a single source file. To help structure and manage large programs, Python allows the user to create their own modules. These modules can be used to group class definitions and related functions and then imported as needed. Typically, a large program consists of a top-level source file and one or more supplemental modules. The top-level file acts as the driver which contains the statements for the main flow of execution. The modules then contain related components which act as tools that are used as needed. Structured ProgramThe following example illustrates the use of modules to organize and structure a program. It consists of three Python source files: driver.py, modA.py, and modB.py. # driver.py # The driver file which contains the statements creating # the main flow of execution. import modA import modB value1 = int( raw_input( "Enter value one: " ) ) value2 = int( raw_input( "Enter value two: " ) ) resultA = modA.funcA( value1, value2 ) resultB = modB.funcB( resultA ) print "Results = ", resultA, resultB # --------------------------------------------------------- # modA.py # A user-defined module which defines a function and # imports a standard module. from math import * def funcA( x, y ) : d = sqrt( x * x + y * y ) return d # --------------------------------------------------------- # modB.py # A second user-defined module which defines a function and # imports the other user-defined module as well as a # standard module. from modA import * def funcB( a ) : b = funcA( a, a * 4 ) return b / 3 + funcC() def funcC( x = 0 ) : return x * 12 The structure of this program is illustrated graphically in the following diagram. Multiple InclusionsModule Program ExecutionTo execute the program, you would enter the following python driver.py at the command-line prompt since the driver program is the main file containing the starting point of execution. Importing The ModulesThe modules are simply Python source files and thus have a As you learned earlier, Python includes a standard library containing modules of functions and class definitions which we have used in a number of our programs. By using modules, the language itself can remain relatively small while still providing extended functionallity. Python programs can quickly become very large and unmanagable if the entire user code is placed within a single source file. To help structure and manage large programs, Python allows the user to create their own modules. These modules can be used to group class definitions and related functions and then imported as needed. Typically, a large program consists of a top-level source file and one or more supplemental modules. The top-level file acts as the driver which contains the statements for the main flow of execution. The modules then contain related components which act as tools that are used as needed. Structured ProgramThe following example illustrates the use of modules to organize and structure a program. It consists of three Python source files: driver.py, modA.py, and modB.py. # driver.py # The driver file which contains the statements creating # the main flow of execution. import modA import modB value1 = int( raw_input( "Enter value one: " ) ) value2 = int( raw_input( "Enter value two: " ) ) resultA = modA.funcA( value1, value2 ) resultB = modB.funcB( resultA ) print "Results = ", resultA, resultB # --------------------------------------------------------- # modA.py # A user-defined module which defines a function and # imports a standard module. from math import * def funcA( x, y ) : d = sqrt( x * x + y * y ) return d # --------------------------------------------------------- # modB.py # A second user-defined module which defines a function and # imports the other user-defined module as well as a # standard module. from modA import * def funcB( a ) : b = funcA( a, a * 4 ) return b / 3 + funcC() def funcC( x = 0 ) : return x * 12 The structure of this program is illustrated graphically in the following diagram. Multiple InclusionsModule Program ExecutionTo execute the program, you would enter the following python driver.py at the command-line prompt since the driver program is the main file containing the starting point of execution. Importing The ModulesThe modules are simply Python source files and thus have a NamespacesAll identifiers in Python live within a namespace — the context in which identifers are defined. You can think of a namespace as a container or index in which various identifiers are stored. When an indentifer is referenced in a Python program, a search is performed in particular namespace to determine if that identifier is valid. The namespace concept allows us to create duplicate names, each existing in a different namespace. In Python, each module constitutes its own namespace. Any identifier declared within a module, may be freely used within that same module. Consider the def funcB( a ) : b = funcA( a, a * 4 ) return b / 3 + funcC() which is valid since both are in the same namespace. But what about the call to from modA import * When the Next, let’s examine the first two statements in the driver.py file import modA import modB These statements include the indicated modules and make their contents available for use within the driver.py file. But, there is a major difference between this version and the Identifers defined within a different namespace, which have not been made a part of the current namespace can still be accessed. In the driver file, we make calls to resultA = modA.funcA( value1, value2 ) resultB = modB.funcB( resultA ) But you will notice the function name is preceded with the module name in which it was defined. By doing this, we tell the interpreter that we are referencing You may be asking why there are two methods to import modules. Why not just use the # --------------------------------------------------------- # main.py import modA import modC x = int( raw_input( "Enter value one: " ) ) y = int( raw_input( "Enter value two: " ) ) v = modA.funcA( x, y ) w = modC.funcA( x, y ) # --------------------------------------------------------- # modA.py from math import * def funcA( x, y ) : d = sqrt( x * x + y * y ) return d # --------------------------------------------------------- # modC.py def funcA( x, y ) : return x + y We want to reference both identifiers from within the driver program. If we had imported the indentifiers into the current namespace, then only the second one imported be known within the current namespace. By including the namespace as part of the reference, we direct the interpreter to the correct identifier. The example program presented earlier in this chapter consists of three modules: | |
© 2006 - 2008: Rance Necaise - Page last
modified on September 10, 2006, at 03:12 PM
|