Creating Python Modules: with Simple Steps


Creating Python Modules


In Python we look at modules ,Module are very simply to put and Module is allows to better organizing the code or pretty much define classes ,functions and variables.

Module is nothing but a block which contain line of codes .Module is easy to import in the program and use its functionality of module.In module we will take any syntax which will use into a program. We create a simple module example to easy understanding.


Now our task is to creating python modules whose name is module_test .


#!/usr/bin/env python
 def add(a,b):
 return a+b

Creating Python Modules: Why Use Module

  • Module is simple provide an easy to organized components into system. module is such a python functionality to easy to maintain the code .you can create an module in txt file and change the extension to “.py”.
  • Module is define own functionality those functionality is easly use into other programs . User can use the simple module file just writing and running an import or from statement.Both of these above statement is find ,compile and running a module .
import Statement

>>> import module_1

>>>module_1.printer('Hello Buffercode')

Hello Buffercode

In above example module_1 is identify an external file to be loaded and it is also become an variable in script and it is refers as module object after execution.Because of import name is refer an module object so module name to fetch its attribute (  module_1.printer ).

from Statement

Now we taken an another example to show how to use from statement

From is also copy the name from one file  over to another scope

>>>from module_1 import printer                              # copy out one variable

>>>printer('Hello Buffercode')

Hello Buffercode

this example is same output as the above example but some statement in the program is can import the module but it can add extra step that copy once and more name .


from* statement

In python is a special type of function it like as from statement but little difference between from and from * statement we get copy out all the name assign at the top level of the reference module.

>>>from module_1 import *

>>>printer('Hello Buffercode')

Hello Buffercode


Have something to add on Creating Python Modules  ? Please add in comments.

Follow us on Facebook, Google Plus and Twitter to get more Tech News and reviews.