PYTHON MODULES Modules in a Nutshell Modules are
![PYTHON MODULES PYTHON MODULES](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-1.jpg)
![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](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-2.jpg)
![Module Example $ vim mymodule. py # a simple module def square(x): “”” Returns Module Example $ vim mymodule. py # a simple module def square(x): “”” Returns](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-3.jpg)
![Using a module >>> import mymodule >>> y = mymodule. square(2) >>>y 4 >>>mymodule. Using a module >>> import mymodule >>> y = mymodule. square(2) >>>y 4 >>>mymodule.](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-4.jpg)
![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](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-5.jpg)
![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](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-6.jpg)
![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.](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-7.jpg)
![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](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-8.jpg)
![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](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-9.jpg)
![. 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](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-10.jpg)
![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__',](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-11.jpg)
![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](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-12.jpg)
![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 …](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-13.jpg)
![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](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-14.jpg)
![Questions? Questions?](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-15.jpg)
![Source Python v 2. 6. 4 documentation >> The Python Tutorial >> Modules http: Source Python v 2. 6. 4 documentation >> The Python Tutorial >> Modules http:](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-16.jpg)
- Slides: 16
![PYTHON MODULES PYTHON MODULES](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-1.jpg)
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](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-2.jpg)
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 squarex Returns Module Example $ vim mymodule. py # a simple module def square(x): “”” Returns](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-3.jpg)
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 square2 y 4 mymodule Using a module >>> import mymodule >>> y = mymodule. square(2) >>>y 4 >>>mymodule.](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-4.jpg)
Using a module >>> import mymodule >>> y = mymodule. square(2) >>>y 4 >>>mymodule. square(y) 16
![Call by Value 49 7 import mymodule x7 mymodule squarex x Call by Value >>> >>> 49 >>> 7 import mymodule x=7 mymodule. square(x) x](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-5.jpg)
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](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-6.jpg)
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.
![fromimport Statements You can import specific names from a module with a fromimport statement from/import Statements You can import specific names from a module with a from/import statement.](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-7.jpg)
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 dont really handle function calls by reference Programmers The Importance of Returning Python modules don’t really handle function calls by reference. Programmers](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-8.jpg)
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 squarex print x x if Modules as Scripts $ vim mymodule. py def square(x): print x * x if](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-9.jpg)
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](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-10.jpg)
. 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 youve imported dir builtins doc name dir() is useful for checking what modules you’ve imported. >>> dir() ['__builtins__', '__doc__', '__name__',](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-11.jpg)
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](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-12.jpg)
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 …](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-13.jpg)
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](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-14.jpg)
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?](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-15.jpg)
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:](https://slidetodoc.com/presentation_image_h2/42b0307c1e46cd67f388b50257ace4fd/image-16.jpg)
Source Python v 2. 6. 4 documentation >> The Python Tutorial >> Modules http: //docs. python. org/tutorial/modules. html
Mikael ferm
Zootopia carrots
Ilya sutskever
Grammar in a nutshell
Ap style vs mla
James webb telescope
Photosynthesis in a nutshell
Tenses in a nutshell
Bfs in a nutshell
Web design in a nutshell
Itil in a nutshell
Modules in operating system
Nys math modules
Extension in hybris
Six phases of public financial management
Meaning of gifmis
Modules wiskunde