PowerPoint Presentation
Basics in programming
Modular programming refers to the process of breaking a large, unwieldy programming task into separate, smaller, more manageable subtasks or modules.
Individual modules can then be cobbled together like building blocks to create a larger application.
Python modules
There are actually three different ways to define a module in Python:
1. A module can be written in Python itself.
2. A module can be written in C and loaded dynamically at run-time, like the re (regular expression) module.
3. A built-in module is intrinsically contained in the interpreter, like the itertoolsmodule.
Only 1. is in scope of this course
Python modules
A module’s contents are accessed the same way in all three cases: with the importstatement.
When creating a module all you need to do is create a file that contains legitimate Python code and then give the file a name with a .py extension.
Python modules
The Module Search Path
-what happens when Python executes the statement:
import SOMETHING
When the interpreter executes the above import statement, it searches for SOMETHING.py in a list of directories assembled from the following sources:
Python modules
The directory from which the input script was run or the current directory if the interpreter is being run interactively
2. The list of directories contained in the PYTHONPATH environment variable, if it is set. (The format for PYTHONPATH is OS-dependent but should mimic the PATH environment variable.)
3. An installation-dependent list of directories configured at the time Python is installed
Python modules
The resulting search path is accessible in the Python variable sys.path, which is obtained from a module named sys:
import sys
sys.path
Python modules
You can put the module file in any directory of your choice and then modify sys.path at run-time so that it contains that directory. For example, in this case, you could put mod.py in directory C:\Users\jari and then issue the following statements:
sys.path.append(‘C:\Users\jari’)
sys.path
Python modules
The import Statement
Module contents are made available to the caller with the import statement. The import statement takes many different forms, shown below.
import
Python modules
Import does not make the module contents directly accessible to the caller.
Each module has its own private symbol table, which serves as the global symbol table for all objects defined in the module.
Thus, a module creates a separate namespace, as already noted.
Python modules
The statement import
The objects that are defined in the module remain in the module’s private symbol table.
From the caller, objects in the module are only accessible when prefixed with
Python modules
Create module mod.py with following variable
s = ”This in modules private symbol table”
Now import mod.py to main application and try to access ”s”
import mod.py
print(s)
Python modules
But if you refer to mod namespace you are able to access ”s”
import mod.py
print(mod.s)
Python modules
Its possible to import multiple libraries at once:
import
Python modules
An alternate form of the import statement allows individual objects from the module to be imported directly into the caller’s symbol table:
from
from mod import s
print(s)
Python modules
An alternate form of the import statement allows individual objects from the module to be imported directly into the caller’s symbol table:
from
from mod import s
print(s)
Python modules
Because this form of import places the object names directly into the caller’s symbol table, any objects that already exist with the same name will be overwritten
it is even possible to indiscriminately import everything from a module at once:
from
this will place the names of all objects from
Python modules
It is also possible to import individual objects but enter them into the local symbol table with alternate names:
from
Python modules
You can also import an entire module under an alternate name:
import
import mod as my_module
print(my_module.s)
Python modules
For reasons of efficiency, a module is only loaded once per interpreter session.
That is fine for function and class definitions, which typically make up the bulk of a module’s contents.
But a module can contain executable statements as well, usually for initialization.
Be aware that these statements will only be executed the first time a module is imported.
Python modules
Create following mod_a.py
a = [100, 200, 300]
print(”a =”, a)
Try importing this multiple times
Python modules
Python modules
Python modules
Python’s from statement lets you import specific attributes from a module. The from .. import .. has the following syntax :
Python modules
You can use built-in dir-function to list all the modules, variables and functions that are defined in a module
Python modules
Exercise, create a program that uses modules. Modules are:
Ship
Person
Ticket
Create a program that creates three ships and assigns passengers, captain and cook into each ship.
Ship has properties like, speed, heading, destination. And person have properties like, role, age and ticket
If person is passenger he/she is not able to enter ship without valid ticket.
Python modules
Create application that uses modules to build a houses and rooms. In main application build three houses and print their full properties. Create modules for house (with some properties) and room (with some properties)
House 1, has three rooms
House2, has four rooms
House3, has five rooms
Exercise 10
/docProps/thumbnail.jpeg