Modules


A module is a file containing Python definitions(e.g functions, classes) and statements.

Module Types:

1. Built-in Modules

       e.g: math, string, date, random etc..

2. User-Defined Modules

       To create a module in Python just save the source code file with extension .py.

Example :

Save this code in a file named mymodule.py
def myFunc(): # function definition
    print("function in module")

myFunc() # function calling
print("statement 1") 

Use a Module:

Before using any module, first, we have to import that module.

There are different ways to import

    1. import  module_name
    2. from  module_name  import  name(s)
    3. from  module_name  import  name  as  alt_name
    4. import  module_name  as  alt_name 

1. The import statement

We can import any python module by using import statement in any other python source file. We can write import statement at any place in source file before using it, generally we write at the top of the file. As python interpreter encounters an import statement, it imports the module from the specified path.

Example :

file: mymodule.py
def myfun1():
    print("function1 from mymodule")
    
def myfun2():
    print("function2 from mymodule") 

Now, create another python file and use mymodule.

file: mymodule1.py
import calendar # import built-in module
import mymodule # import user defined module

'''call the functions by using
module_name.function_name'''

mymodule.myfun1() 
print(calendar.month(2020,4))# print the month of year
print(calendar.isleap(2020)) # check leap year
                
OUTPUT
function1 from mymodule
        April 2020
Mo Tu We Th Fr Sa Su
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
         
True

2. The from import statement

The from import statement, import only specific attributes from the module.

Syntax:

from  module_name  import  name(s)
                

Example:

from calendar import month

'''Now, not need to call function by 
module name, direct call the imported attributes'''

print(month(2020, 4))

Here, we imported only the month function from the calendar module.

In such cases, we don't use the dot operator to call any attribute. We can also import multiple attributes as follows:

from calendar import month, isleap

print(month(2020, 4))
print(isleap(2020))

The from import * Statement

By using * in from import statement will import all the names from the module.

Syntax:

from  module_name  import  *
                

Example:

from calendar import *

print(month(2020, 4))
print(isleap(2020))

Here, we have imported all the definitions from the calendar module. This includes all names visible in our scope except private members(names starts with double underscore).

Generally, we avoid to import everything with the asterisk (*) symbol because it is not a good programming practice. Sometimes this lead to ambiguous definitions for an identifier and also less readable.


3. Import with renaming

We can import a module by renaming it as follows:
import calendar as cl

print(cl.month(2020, 4))
print(cl.isleap(2020))
                

We have renamed the calendar module as cl. Now, we will use cl to call the members instead of calendar.

We can also rename the imported specific function as follows:

from calendar import month as mn

print(mn(2020, 4))
 

In the above condition, instead of writing month to execute the function we will write mn.

The dir() function:

dir() is a built-in function that list all the function names (or variable names) in a module.

Example

import calendar

print(dir(calendar))
                
OUTPUT
['Calendar', 'EPOCH', 'FRIDAY', 'February', 'HTMLCalendar', 'IllegalMonthError', 'IllegalWeekdayError', 
'January', 'LocaleHTMLCalendar', 'LocaleTextCalendar', 'MONDAY', 'SATURDAY', 'SUNDAY', 'THURSDAY', 'TUESDAY',
 'TextCalendar', 'WEDNESDAY', '_EPOCH_ORD', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', 
'__name__', '__package__', '__spec__', '_colwidth', '_locale', '_localized_day', '_localized_month', '_monthlen', '_nextmonth', 
'_prevmonth', '_spacing', 'c', 'calendar', 'datetime', 'day_abbr', 'day_name', 'different_locale', 'error', 'firstweekday', 'format', 
'formatstring', 'isleap', 'leapdays', 'main', 'mdays', 'month', 'month_abbr', 'month_name', 'monthcalendar', 'monthrange', 'prcal', 'prmonth', 
'prweek', 'repeat', 'setfirstweekday', 'sys', 'timegm', 'week', 'weekday', 'weekheader']

Next chapter is Anonymous Function





Video Lecture



 




Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017, prowessapps.in, All rights reserved