Tuesday 7 January 2020

Modules in Python

#f:/Programs/generate/cali.py
#cali.py
def add(a,b):
    return a+b
def sub(a,b):
    return a-b
def mul(a,b):
    return a*b
def div(a,b):
    return a/b

Save the above program as ‘cal.py’
write another program in the same folder
#f:/Programs/generate/cali.py
import cal
#a,b=(int(x)for x in input("enter 2 numbers:").split())
a=int(input("enter a value "))
b=int(input("enter a value "))
c=cal.add(a,b)
d=cal.div(a,b)
print(c)
print(d)
"""output:
enter a value 4
enter a value 6
10
0.6666666666666666
"""
#In case module  is in a different directory f:/Programs/generate
import sys
sys.path.append('f:/Programs/generate')
import cal

#a,b=(int(x)for x in input("enter 2 numbers:").split())
a=int(input("enter a value "))
b=int(input("enter a value "))
c=cal.add(a,b)
d=cal.div(a,b)
print(c)
print(d)
"""output:
enter a value 4
enter a value 6
10
0.6666666666666666
"""
We can use from module_name(fileName) import functionname1,functionname2
#In case it is a different directory
import sys
sys.path.append('f:/Programs/generate')
from cal import add,div

#a,b=(int(x)for x in input("enter 2 numbers:").split()) #To read the single #statement
a=int(input("enter a value "))
b=int(input("enter a value "))
#c=cal.add(a,b)
#d=cal.div(a,b)
c=add(a,b)
d=div(a,b)
print(c)
print(d)
"""output:
enter a value 4
enter a value 6
10
0.6666666666666666
"""

Defn: A group of functions, variables, and classes saved to a file, which is nothing but module. Every python file (.py) is nothing but a module.
In this program, cal.py contains 2 functions.

There are two types of modules: 
(1.) Predefined modules
(2) user-defined module

Predefined module (built-in module): sys,io.time,etc
The user-defined module means we have to create our own module and use them whenever we need them.
Once the module is created, any developer in the project team can use that module.

Advantages:
1.     Reusability: code can be reuse
2.     Save the time

'from..import'

We can import the modules, and its members by using 'from..import' keywords. Main use of 'from..import' directly we can access members of modules.

Syntax: from module import function1,fun2,mem
In case single function:
from module import function

Example:
from ex2 import x,add,mul
print(x)
add(2,3)
mul(2,3)

"""
output:
20
sum= 5
Product= 6
 """

Note: whenever we are using a module in our program, for that module compiled file will be saved in the hard disk.
Renaming a module:

import ex2 as e
print(e.x)
e.add(2,3)
e.mul(2,3)

Here ex2 is the original module name and e is the alias name.
We can access members (functionames, variables,classnames) by using alias name ‘e
We can import all members of a modules by using following way:

from module1 import*
print(n)
add(2,3)
mul(2,3)
"""
20
5
6
"""

If you want to use the other program’s functions, variables, classes in another program:

import module
import module1,module2,module3,module4,….
import module1 as m
Renaming Multiple modules :
Import module1 as m, module2 as m1, module3 as m2,..

Without using module name, directily we can call the members of modules:
From module1 import add,n,mul

#all the members can be accessed by using *

From module1 import*

Related Video: https://youtu.be/9FgKnHHw09g

Next: dir() function and
https://youtu.be/DZpN8_7i9l8
Prev: https://youtu.be/pNpmiKhOhKQ
Generators

No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(&qu...