1 / 15

Python Modules

Python Modules. What are Python modules?. Files with previously written code that can be imported to the Python interpreter. A module contains its own public and private variables to prevent interferences with user defined ones. Modules file have . py extension. An example of a module.

jenis
Download Presentation

Python Modules

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Modules

  2. What are Python modules? • Files with previously written code that can be imported to the Python interpreter. • A module contains its own public and private variables to prevent interferences with user defined ones. • Modules file have .py extension

  3. An example of a module • $ vim modex.py • def cube(x): • //Returns the value of x^3 • return x*x*x,

  4. Importing Modules • >>> import modex • >>> modex.cube(6) • 216 • >>> y = modex.cube(6) • >>>y • 216

  5. Accessing global variables in a module • Using dot notation. • >>> a = anymodule.globalvar • Note that a global variables will not affect another module; i.e. modify module1.gvar will not change the value of module2.gvar.

  6. From/import statements • Used to import specific functions from a module • >>> from modex import cube • >>> cube(3) • >>>27

  7. Executing modules as scripts • Python modules can be executed in command line whith • python modex.py <arguments>

  8. Compiled Python files • When importing modex.py, a modex.pyc file is generated. • It contains the bytecode from the last time modex.py is imported. • If there is no .pyc file exists, it is created • For future imports, these .pyc files are used to speed up the loading time, not the execution time

  9. The dir() function • This function returns a list of modules and functions that have been imported • >>> dir() • [‘_ _name_ _’, ‘_ _package_ _’] • >>>import modex • >>>dir() • [‘_ _name_ _’, ‘_ _package_ _’,’_ _modex’]

  10. Standard Modules • Python comes with a library of standard modules. • These modules contains built-in operations that are not actually part of Python’s core • Some of them are only imported based on the OS. For instance: winreg module is only provided on Windows systems. • An important Std module is sys. it is used to change the search path for the module • >>> sys.path.append(‘/home/pnp29/cs265_spring_2011

  11. Packages • They are organized collections of modules. • Modules are stored in directories • Each directory contains a __init__.py file which tells the Python interpreter that it is a package directory.

  12. Example of a package • math/ • __init__.py • triArea.py • sqArea.py • statistics/ • __init__.py • meanVal.py

  13. Example of using packages • >>> import math.sqArea • >>> math.sqArea.area(5) • 25 • >>> from math.sqArea import area • >>> area(5) • 25

  14. Questions?

  15. Sources • http://docs.python.org/tutorial/modules.html

More Related