Modular Programming is a design technique to break your code into different parts. These parts in which we are breaking code into are called modules.
PART - I
PART - II
mymodule
¶mymodule.py
file as shown below in the current working directory, i.e., the directory in which this IPython Notebook file resides.AUTHOR = 'Arif Butt'
BATCH = 2021
def myfactorial(num):
# code
def myindex(numbers, no):
# code
def mysort(numbers, inplace=False):
# code
mymodule
¶import mymodule as mm
print(dir(mm))
['AUTHOR', 'BATCH', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'myfactorial', 'myindex', 'mysort']
mm.AUTHOR
mm.mysort
mm.__name__
mm.__cached__
.pyc
files are created by the Python interpreter when a.py
file is imported.- The
.pyc
file contain the compiled bytecode of the imported module, so that the "translation" from source code to bytecode (which only needs to be done once) can be skipped on subsequent imports to speed up startup.- If the
.pyc
is older than the corresponding .py file, we do have to re-import it in our program.- The
.pyc
file is still interpreted, however, cnce the.pyc
file is generated, there is no need of.py
file, unless you edit it.
mm.__builtins__
Let us use the factorial()
function
mm.myfactorial(5)
Let us use the myindex()
function
list1=[44, 99, -65, 101, 27, 88]
mm.myindex(list1, 101)
3
Let us use the mysort()
function. Remember the default inplace argument is False, so it does not modify the list that is passed rather returns a new sorted list
list1=[44, 99, -65, 101, 27, 88]
rv = mm.mysort(list1)
rv
[-65, 27, 44, 88, 99, 101]
list1
[44, 99, -65, 101, 27, 88]
Let us use the mysort()
function and pass inplace
argument as True, so it sort the list that is passed and returns None
list1=[44, 99, -65, 101, 27, 88]
rv = mm.mysort(list1, inplace=True)
type(rv)
NoneType
list1
[-65, 27, 44, 88, 99, 101]
Let us try to import a module named arifmodule
that is located in the following directory
/Users/arif/Documents/0-DS-522/Demo-Files-Repo/Section-2-Basics-of-Python-Programming/Lec-2.15-Creating-Python-Modules-and-Packages/module-files/pathissue/
import arifmodule
- First Python Interpreter looks for the module in the current working directory, i.e., the directory from which the input script was run
- Let us see the contents of current working directory
import os
os.listdir()
So in the current working directory, we do not have the module named arifmodule
- Second Python Interpreter looks for the module in the list of built-in modules of Python standard library
- Let us see the list of all builtin modules
print(dir(__builtins__))
So arifmodule
is not in the list of __builtins__
- Third If there is no built-in module of that name, Python looks into a list of directories defined in
sys.path
environment variable- Let us see the directories mentioned in the
sys.path
environment variable
import sys
sys.path
The module arifmodule
is not in either of the above locations
- Let us now add the path of the directory containing
arifmodule
in thesys.path
environment variable
import sys
sys.path.append('/Users/arif/Documents/0-DS-522/Demo-Files-Repo/Section-2-Basics-of-Python-Programming/Lec-2.15-Creating-Python-Modules-and-Packages/module-files/pathissue/')
Now let us try to import arifmodule
again
import arifmodule
Success
demomodule.py
located in the same directory in which this .ipynb file is located.print("This is the demomodule having only one print statement. It is located in the same directory in which this .ipynb file is...")
# import the module for the first time
import demomodule
Let us import this module again
import demomodule
# you can note that python imports the module only once
What if we have made some changes in this module and want to reload it, there are two ways to do that:
imp.reload(<modulename>)
functionimport imp
imp.reload(demomodule)
import demomodule
main()
Function in Python¶main()
function serves as a starting point for the execution of a program. It is executed automatically every time the program is run.main()
, because the Python interpreter executes from the top of the script file unless a specific function is defined.__name__
Variable and Python Execution Modes¶.py
file may be executed as a script or may be imported as a module in another script.__name__
, that helps us decide whether we want to run the script or we want to import the functions defined in the script__name__
variable equals __main__
__name__
variable will contain the name of the script.myscript.py
:def myFunction():
print('The value of __name__ is ' + __name__)
def main():
myFunction()
if __name__ == '__main__':
main()
myscript.py
as a Python Script¶!cat myscript.py
%run myscript.py
So it proves that when we run the
myscript.py
file as a script the__name__
variable contains__main__
, the condition evaluates to True and thereforemain()
function is called which further caledmyFunction()
, which executes the print statement.
myscript.py
in another script as a Python Module¶!cat myscript.py
import myscript as ms
So it proves that when we import the
myscript.py
file as a module the__name__
variable DOES NOT contains__main__
, the condition evaluates to False and thereforemain()
function is NOT called and therefore nothing is printed on screen.
Now since the myscript.py
has been imported, so we can call its functions. Let us call myFunction()
ms.myFunction()
So it proves that when we import the myscript.py
file as a module the __name__
variable contains the name of the script __myscript__