62 63 64
#--------------------------------
#--------- PHY 62----------------
#--------------------------------
Modules
a group of functions , classes, variables saved in a file called Modules.it is a phython file.
Every phyton file is a module.
x=888
name ='afs'
def add(a,b):
print(a+b)
def product(a,b):
print(a*b)
--- save this file as abc.py then in another file import abc(modulename)
modulename.variablename
modulename.functioname()
biggest advantage is we can use existing module in our file.
Advantage:
Code Reusability
Readibiltiy
Development time
Cost of Project
Both file should be in same directory ? generally yes but it can be in another folder also.
Packages is next level concept.
From import ?
for module pyc file generated . if code changed then pyc file will generated again.
#--------------------------------
#--------- PHY 63----------------
#--------------------------------
in last session
what is module
advantage of module
how to define module and how we can use that module?
Module name aliasing :
==================
import abc as a
a.variablename
a.functionname()
once we define the aliasname then we cannot use original name.
if i donot want to use modulename or aliasname then we have to use From import.
Usually we can acccess members of a module by using module na,e.
If we donot want to use module name then we should go for FROM IMPORT.
From Modulename import x, name
now we can directly use x and name but now cannot use function.
All memeber from module
From modulename import * ------------ important
Anywhere we cab write import statement.
Member Aliasing
===========
From durgamath import addd as a , product as p
Various Possibilities of Import:
===============================
import module1
import module1, module2, module3
import module1 as m1
import module1 as m1 ,module2 as m2 , module3 as m3
from module1 import member1
from module1 import memeber1, member2, member3
from module1 import *
from module1 import member1 as m1
from module1 import member1 as m1, memeber2 as m2
Module Naming conflicts;
========================
module1
x=10
def add(a,b):
print('module1')
print(a+b)
module2
y=20
def add(a,b):
print('module2')
print(a+b)
---
from module1 import *
from module2 import *
add(10,20)----------------------------python is always going to consider the most recent copy
In the case of conflicts, python will always consider the most recent copy.
from module1 import *
from module2 import *
def add(a,b):
print('currnet module')
print (a+b)
add(10,20)
the most recent copy --order wise which is available at last.
x=10
x=20
x=30
print(x)----30
x
First way
import module1
import module2
module1.add(1,2)
module2.add(1,2)
second way
from module1 import add as a1
from module2 import add as a2
a1(1,20)
a2(22,33)
Reloading of a module:
======================
if you import module multiple times in code , it will be loaded just once.
By default module will be loaded only once eventhough if we are importing multiple times.
Advantage : Performance is more.
Problem : We are missing updations of that module.
there is a imp module having reload function. it is used to reload the module in code.
#--------------------------------
#--------- PHY 64----------------
#--------------------------------
Module
Advantage of module
various possibilittes of import
Naming conflicts
module reloading
dir() and help() Functions.
Finding memebers of module by using Dir() function :
===================================================
To list out member of a module.
dir() ---> to list members of current module.return as list
dir(modulename) --to listout members of specified module.
a=10
b=20
def add(a,b):
return a+b
def product(a,b):
return a*b
print(dir()) --- will return ['a','b','add','product']
For every module Python virtual machine will add several predefined variables which are internally used by PVM. Even these variables can be used programmer based on his requirenment.
to list out all memeber of MAth module
dir(math) --- befire using we need to import math module
import math
print(dir(math)) ---- it will list out all memmber of module varibale + mehtod now how to identify
help documentation of a module
=================================
help()
how to use help
import math
help(math) --- no need to use printm , using help we can identity which is variable and method.
Difference between Dir() and help()
===============================
Dir() is just to get all list of memeber as a list
help() will give complete documentation of module
Extra member added by PVM for every module:
============================================
__doc__
__file__
__name__
__loader__
.
.
print(__annotation__)
__doc__
========
this variable hold documentaiton string.
import math
print(math.__doc__)
import random
print(random.__doc__)
__File__
=========
print(__file__) --- willl return file name
Absolute path to get --
print('Absolute Path',os.path.abspath(__file__))
Comments
Post a Comment