Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modules. Modules Modules are the highest level program organization unit, usually correspond to source files and serve as libraries of tools. Each file.

Similar presentations


Presentation on theme: "Modules. Modules Modules are the highest level program organization unit, usually correspond to source files and serve as libraries of tools. Each file."— Presentation transcript:

1 Modules

2 Modules Modules are the highest level program organization unit, usually correspond to source files and serve as libraries of tools. Each file is a module and modules import other modules to use the name they define. import import: lets a client fetch a module as a whole from from: allows to fetch particular names from module reload reload: provides a way to reload code without stopping Python

3 Why Use Modules? Modules provide an easy way to organize components into a system, by serving as packages of names Code reuse: code in module is persistent: can be reloaded and run as many times as needed System namespace partitioning: Everything “lives” in a module: code and objects are always implicitly enclosed by a module Implementing shared services or data

4 Structure a Program General architecture General architecture: Multiple text files with Python statements One top-level file with zero or more modules (imports) Top-level file contains the main control flow import A file imports a module to gain access to the tools it defines

5 Structure a Program Attributes Attributes are tools defined by a module (functions). in b.py: def spam(t): print t, in c.py: import b object.attribute b.spam(“hi”) # object.attribute

6 How Imports Work module search path: Find the module using module search path: Home directory of top-level file PYTHONPATH directories Standard directory libraries The contents of any *.pth (path configuration) files. It checks: source file(*.py), byte-code file(*.pyc), a directory for package imports, C extension module, in-memory image (frozen executables), java class, zip file(zipimport module)

7 How Imports Work Compile it to byte-code (if needed) *.pyc is saved only for imported modules Run its code to build the objects it defines Future imports skip all three steps and reuse already-loaded module

8 Modules Usage Clients can use the module file we just wrote by running “import” statement >>> import math >>> import random >>> import module1 # assume this is the file name we saved Clients can use the module file by running from statement >>>from fib import fib, fibTopN, list without module The copied names are used directly without going through module

9 Modules examples We create a file name module1: def print_out(aa): print aa*3 x=1 y=2

10 Modules Example now we write another file named module2 import module1 module1.print_out(“Hello World!! ”) # Use module1’s function print module1.x, module1.y # Reference module1’s variable x=10 y=20 print x, y The result of execute this program is: Hello World!! Hello World!! Hello World!! 1 2 10 20

11 Modules Example You may import as many modules as you like, for example: import module1 import module2 print module1.x print module2.x The result of execute this program is: 1 10


Download ppt "Modules. Modules Modules are the highest level program organization unit, usually correspond to source files and serve as libraries of tools. Each file."

Similar presentations


Ads by Google