Saturday, 8 April 2017

Modules concept in python

Module


  • Every Python file itself is known as a Module.
  • A Python file can contain local variables, functions, classes and executable statements.
  • A file containing Python code, for e.g. example.py, is called a module and its module name would be example .
  • modules provide re-usability of code
  • We can use the properties of one module to another module by importing that module.
example: addition.py

def add(a, b):
   """This program adds two
   numbers and return the result"""

   result = a + b
   return result

note:
here module name is filename -----> addition


Normal import:-


  • Whenever we import any module by using normal import statement we can access the properties of that module by using the module name  only.
import addition
print(addition.add(4,5))

9

Python has a ton of standard modules available. You can check out the full list of Python standard modules and what they are for. These files are in the Lib directory inside the location where you installed Python. Standard modules can be imported the same way as we import our user-defined modules.
import math
print(dir(math))
print("-------------------")
help(math.sqrt)
print("-------------------")
help(math.ceil)
print("-------------------")
help(len)
print("-------------------")
help(dir)

dir:-

  • It is a pre-defined function, which is defined in builtins module.
  • dir function is used to  know the information about given module.
  • help (dir( ))


If we don't pass any module name as a parameter to the dir( ) function then it will provide the information about current module.

Note:-
  • Which Python file directly we are running that Python file technically we call as a main module.
  • We can know the information about the particular property of particular module by using help( ) function.

import math
p=123
def m1( ):
    print("HI")
print(dir( ))
print("---------------")
print(dir(math))
print("---------------")
print(math.sqrt)
print("---------------")
print(math.ceil)

Renaming a module:-

  • Instead of accessing the properties  of a module always by using module name whenever we use normal import statement we can create  alias name for a module and access the properties of that module by using alias name.
  • After creating the alias name or rename for a module we are not allowed to access the properties of that module by using its original name.

import math as a
import addition as b
print(a.pi)
print(b.add(4,5))
print(a.sqrt(9))

3.141592653589793
9
3.0

Note:
  • in this case accessing the properties of that module by using module name or alias name, this is a problem.to over come this problem we are going to concept called from import.
from import:
  • when ever we import a module by using from import concept,we can access the properties of that module directly with out using module name or alias name. 

from math import pi,sqrt
from addition import add
print(pi)
print(add(4,5))
print(sqrt(9))

3.141592653589793
9
3.0

from import with  *

  • It is used to make it available all the properties of one module into another module at a time.
  • from import with * degrades the performance of applications,so that never recomonded to use.

from math import *
from addition import *
print(pi)
print(add(4,5))
print(sqrt(9))

3.141592653589793
9
3.0

builtins module

  • builtins is a pre-defined module, which contains frequently used logic's.
  • In every Python module directly we can access the properties of builtins module, because Python interpreter automatically imports the builtins module into the every Python file.

from builtins import *
x='Python'
print(len(x))
help(len)

6
Help on built-in function len in module builtins:

len(obj, /)
    Return the number of items in a container.

import os
if os.name=='nt':
    command="ipconfig"
else:
    command="pwd"
os.system(command)

Reloading a module

Whenever we import a module for more than once then by default that module is going to be loaded into the memory only once.
In order to load the module into the memory location once again, we call reload function of imp module.

mod1:
print("hi siva")
def f1():
    print("i am in f1 of mod1")
f1()
hi siva
i am in f1 of mod1
mod2:
import mod1
print("begin mod2")
print("end mod2")
hi siva
i am in f1 of mod1
begin mod2
end mod2

mod3:

import mod1
import mod1
print("begin mod2")
print("end mod2")

hi siva
i am in f1 of mod1
begin mod2
end mod2

reloading a module:

import mod1
import imp
print("begin mod2")
print("end mod2")
imp.reload(mod1)

hi siva
i am in f1 of mod1
begin mod2
end mod2
hi siva
i am in f1 of mod1

Modules Search Path

Whenever we import a module then Python interpreter searches for the imported modules in the following order.

1) Current/Present working directory(main module location)

2) Python path(Environment variable)

3)Installation dependent default directory


No comments:

Post a Comment