Python Grammer: Module

 

Basis of Module

A module in Python is a kind of file that serves as a collection of functions that are frequently used. If you use a module, a complex function is separated into a separate file. Therefore, it is possible to create a simple program structure.

The basic syntax of the module is as follows.

import module                                 #(1)

import module, module                      #(2)

from module import function/attribute  #(3)

import module as alias                       #(4)

 

(1) Import: Specify the module to be used with the import statement.

(2) A Plurality of Modules: It is possible to use multiple modules with a comma.

(3) Specifying Function: Specify the module name with “from”. Using “import” after that, specify the name of the function that is to be used.

(4) Using the Alias: It is possible to rename the module using a name that is appropriate for the program features.

You can check the module path that Python recognizes as follows. To save the module to another path, it is necessary to add the path by yourself.

import sys                                                                  #(1)

print sys.path                                                             #(2)

sys.path.append("D:\Python27\Lib\myModule")   #(3)

 

(1) Import sys Module: The “sys” module provides information and functions that are related to the interpreter.

(2) sys.path: Provides the path information that can be used to locate the referenced module.

(3) Add the Path: It is possible to add the path of new module by using the “path.append” function.

Custom Module

In addition to the basic modules that are provided in Python, modules can also be defined by the user. Here, we can learn how to create a custom module through a simple example. For convenience, let’s save the user-defined module in the same directory as the example. The prefix "mod" is used to distinguish it from a general program.

skill = ["sword","spear","bow","axe"]                 #(1)

power = [98.5, 89.2, 100, 79.2]

 

def printItem(inSkill, idx=0):                            #(2)

    name = "Hong Gil Dong"

    age = 18

    weight = 69.3

 

    print "\n"

    print "----------------------------------------"

    print "1.name:", name

    print "2.age:", age

    print "3.weight:", weight

 

    print "4.armed weapon:",inSkill, "[ power", power[idx],"]"

    print ">>>i am ready to fight"

 

modHero.py

(0) Creating a Module: Save it in the same directory as the program that calls the “modHero.py” module.

(1) Declaring Variable: Declare a variable that can be used internally or externally

(2) Declaring Function: Define a function according to the feature that the module provides.

To import a previously declared module, let's create a program that uses the functions in the module.

import modHero                                            #(1)

 

querySkill = raw_input("select weapon: ")

 

i=0

 

for each_item in modHero.skill:                         #(2)

    if(each_item == querySkill):

        modHero.printItem(querySkill, i)              #(3)

    i = i+1

       

print "----------------------------------------"

print "\n"

 

Calling of Module

(1) Import Module: Explicitly import the “modHero” module

(2) Module Variables: Use the “skill” variable that has been declared in the module “modHero”.

(3) Module Function: Use the “printItem” function that has been declared in the module “modHero”.

“sys” module supports the program to recognize the module in a different manner. It can be used in the same way as “sys.path.append(directory)”.

Post a Comment

Previous Post Next Post