The import statement

In order to use the functions and variables of the module1.py program, we will use the import statement. The syntax of the import statement is shown here:

Import module1, module2, module 

In this way, you can import multiple modules. Let's make another program mod1.py, which will import module1.py:

 import module1
x = 12
y = 34
print "Sum is ", module1.sum1(x,y)
print "Multiple is ", module1.mul1(x,y)

As you know, the module contains statements and definitions and these statements and definitions are executed for the first time when the interpreter encounters the module name in the import statement.   

In preceding code, the module1 module gets executed when the interpreter encounters the module1 name in the import statement.  In order to use the module variables and functions, use the module_name.variable and module_name.function() notations. In the preceding code, we want to use the sum1() function of module1, that's why we use module1.mul1().

Let's see the output:

Output of the mod1.py program

If you think it is tedious and time consuming to write module1 with every function of module1.py, then Python allows you to use the as statement as shown. The syntax is given as follows: 

import module_name as new_name

Let's write a program mod2.py:

import module1 as md
x = 12
y = 34
print "Sum is ", md.sum1(x,y)
print "Multiple is ", md.mul1(x,y)

In the preceding code, module1 is used as md. Let's see the output:

Output of the mod2.py program

You can explicitly define the function as per your need. Consider we want only the sum1() function not mul1(). Syntax is given as module-name import function-name

Let's write the code:

 from module1 import sum1
x = 12
y = 34
print "Sum is ", sum1(x,y)

In the first line, I defined from where I am taking the sum1() function, then there will be no need to use module1 with the function name. Let's see the output of the code:

Output of the mod3 program

Consider a module that contains many functions and you have to import all the functions. You can use the following statement as shown:

from module-name import *

But I advise you don't use the preceding statement because if you are importing more than one module, then it is very difficult to identify which function is taken from which module. Let's take an example. Consider one more module named as module2:

def sub1(a,b):
c = a-b
return c

def divide1(a,b):
c = a/b
return c

Now we have two modules module1 and module2. We have defined module1 earlier. Let's write the next program mod4.py:

 from module1 import *
from module2 import *
x = 36
y = 12
print "Sum is ", sum1(x,y)
print "Substraction is ", sub1(x,y)
print "after divide ", divide1(x,y)
print "Multiplication is ", mul1(x,y)

In the preceding function, we imported all the functions of module1 and module2. But it is very difficult to identify which function is coming from which module. The output of the program is as shown here:

Output of the mod4 program

However, Python offers you the built-in function dir(), which can be used to identify the functions. See the example mod5.py:

 import module1
print dir(module1)

The output of the program is as shown here:

The preceding is the output of the mod5 program. After seeing the output, we can say that the mul1() and sum1() functions came from the module1 module.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.47.169