Sunday, October 25, 2020

Module in Python

In this chapter you will learn what is module, how to define a custom module, importing custom module in python program and use them.
Lets start by defining a module in python, Module is piece of code that does a specific functionality in programming. All similar functionalities are grouped into source code files called module. 
For Example math module of python, different math functions like Trigonometric functions, logarithmic functions, angular functions, numeric functions, hyperbolic functions are grouped into a single math module.
All python source file use an extension .py lets create a python source file words.py to read data from a file and convert first letter of each word to caps. 
Copy below code to a file and execute

'''
    Module to read a text file and convert each word 
    first letter to CAPS.
'''

def readfile(fileName):
    '''
       Reads input file passed as an agrument process the data
       and prints capitalized words to console.
       Args:
            Takes fileName as an argument
    '''
    fp = open(fileName, mode='rt', encoding='utf-8')
    words = fp.read().split()
    newWords = convertToCaps(words)
    print('Words in Capitalized form')
    for word in newWords:
        print (word)
    fp.close()
    

def convertToCaps(wordsList):
    '''
       Reads input fila passed as an agrument process the data
       and prints capitalized words to console.
       Args:
            Takes wordsList as an argument
       Returns:
            returns wordsList with capitalized letter in each word
    '''
    capitalizedWords = []
    for word in wordsList:
        capitalizedWords.append(word.capitalize())
    return capitalizedWords

Python module name is same as file name excluding the extension .py , our module name is words as file name is words.py lets import the module and execute it. Module is imported using the import keyword


Download the code from GitHub

Importing the same module times has no effect, once module is imported, to impact any changes made to module after its imported quit python interpreter and import the module on a fresh python interpreter.

Lets create a test module to demonstrate the same. create a file test.py and copy the below code into it

'''
    Module to demonstrate importing same
    module multiple times and its effect
'''
print(__name__) 

we will define __name__ in our next chapter, for now lets execute and see the output.

Code inside module is executed immediately after its imported. When module is imported __name__ is evaluated to module name. Output test is printed only once, Multiple imports of same module has no effect.

Lets execute the module directly and see the output of __name__

Here same __name__ is evaluated to __main__ when executing the module.
This is an important feature of python same __name__ built in type showing different outputs when executing the module vs importing module. __name__ in detailed in our next chapter. 

No comments:

Post a Comment

Operations on Python Dictionaries