Python Modules - College of Computing & Informatics
PYTHON MODULES
Brandon McKay
Reasons for using modules
? Definitions of functions and variables are lost after quitting each session of the Python interpreter
? Creating a script file of your Python input for each individual program would be cumbersome and redundant
? Modules allow you to import code from an external file and use it in your program/script, in a similar fashion as libraries used in C++
Importing modules
? File name is the module's name with .py extension ? The "import" command allows the functions/statements of
the module to be accessed locally
? Import functions/statements of "module.py" >>> import module
? Use a function from "module.py" >>> module.function(argument)
? Copy the function to a local name >>> localFunction = module.function >>> localFunction(argument)
Importing modules (cont.)
? Can import selected portions of the module
? Import selected names in the module as local names >>> from module import function1, function2 >>> function1(argument) >>> function2(argument)
? Import all names except those that start with "_" >>> from module import *
Executing modules as scripts
? When importing modules, the __name__ variable is set to the module's name
? When running a module directly from the command line, the __name__ variable is instead set to __main__
? This allows the ability to execute code depending on whether the module is being used as a script or as an imported module
$ python module.py arguments (__name__="__main__") >>> import module (__name__="module")
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- python modules
- python cheat sheet programming with mosh
- python lists
- chapter 4 python functions modules and packages 14 marks
- distributing python modules university of california berkeley
- python module
- installing python modules university of california berkeley
- python modules college of computing informatics
- basic python by examples ltam
- built in functions and python modules university of iowa
Related searches
- university of minnesota college of education
- college of medicine tucson
- city college of baltimore city
- hong kong college of technology
- american college of education scam
- penn college of technology
- python modules list
- python modules download
- university of south florida college of medicine
- u of m college of continuing education
- university of minnesota college of continuing education
- university of illinois college of nursing