PYTHON MODULES Modules in a Nutshell Modules are

  • Slides: 16
Download presentation
PYTHON MODULES

PYTHON MODULES

Modules in a Nutshell Modules are text files you can import to the Python

Modules in a Nutshell Modules are text files you can import to the Python interpreter in order to reuse previously written code. A module has its own global and local scopes to prevent interference with user defined names. A package is a collection of modules, grouped for organization and easy access.

Module Example $ vim mymodule. py # a simple module def square(x): “”” Returns

Module Example $ vim mymodule. py # a simple module def square(x): “”” Returns the square of its parameter. “”” return x * x

Using a module >>> import mymodule >>> y = mymodule. square(2) >>>y 4 >>>mymodule.

Using a module >>> import mymodule >>> y = mymodule. square(2) >>>y 4 >>>mymodule. square(y) 16

Call by Value >>> >>> 49 >>> 7 import mymodule x=7 mymodule. square(x) x

Call by Value >>> >>> 49 >>> 7 import mymodule x=7 mymodule. square(x) x

Accessing the module variables You can access the global variables of a module using

Accessing the module variables You can access the global variables of a module using dot notation. >>> x = somemodule. somevariable Global variables in a module will not otherwise effect another module, or user defined components.

from/import Statements You can import specific names from a module with a from/import statement.

from/import Statements You can import specific names from a module with a from/import statement. >>> from mymodule import square >>> square(3) 9 >>> from mymodule import square, cube >>> cube(2) 8 >>> from mymodule import *

The Importance of Returning Python modules don’t really handle function calls by reference. Programmers

The Importance of Returning Python modules don’t really handle function calls by reference. Programmers have to work around this with return statements. >>> x = 2 >>> x = mymodule. square(2) >>> x 4

Modules as Scripts $ vim mymodule. py def square(x): print x * x if

Modules as Scripts $ vim mymodule. py def square(x): print x * x if __name__ == "__main__": import sys square(int(sys. argv[1])) ~ ~ $ python mymodule. py 2 4

. pyc Files On a successful import, a modulename. pyc file is generated. This

. pyc Files On a successful import, a modulename. pyc file is generated. This file contains the bytecode from the last time the modulename. py file was imported. The interpreter checks the date modified before using the. pyc file, there is no danger of using an older version. These files only speed up the loading of a module, not the execution.

dir() is useful for checking what modules you’ve imported. >>> dir() ['__builtins__', '__doc__', '__name__',

dir() is useful for checking what modules you’ve imported. >>> dir() ['__builtins__', '__doc__', '__name__', '__package__'] >>> import mymodule >>> from mymodule import square >>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'mymodule', 'square']

Standard Modules import first checks the current directory for the specified module. The interpreter

Standard Modules import first checks the current directory for the specified module. The interpreter then follows a default path like /usr/local/lib/python, or the PYTHONPATH EV. To change the path, import “sys”, one of Python’s standard modules. >>> import sys >>> sys. path. append(‘/home/jm 4564/cs_265’)

A Package Example math/ __init__. py logarithms. py circles. py statistics/ factorial. py …

A Package Example math/ __init__. py logarithms. py circles. py statistics/ factorial. py … …

Importing Packages can be imported in as large, or as small, portions as you

Importing Packages can be imported in as large, or as small, portions as you require. >>> import math. circles >>> math. circles. area(5) 78. 53981634 >>> from math. circles import area >>> area(3) 28. 27433388

Questions?

Questions?

Source Python v 2. 6. 4 documentation >> The Python Tutorial >> Modules http:

Source Python v 2. 6. 4 documentation >> The Python Tutorial >> Modules http: //docs. python. org/tutorial/modules. html