Local and Global Variable,Module,Package in Python

Local and Global Variable,Module,Package in Python

Local and Global Variables

Variables that are defined inside a block are called local variable, and those defined outside block are called global variable. This means that local variables can be used only within the block in which they are declared, whereas global variables can be used in the whole program. In case of local and global variable preference is given to local variable.

 

 Program for demonstration of function with variable arguments

a=2

print(a)

def display():

                     b=3

                     print(a)

                     print(b)

display()

print(a)

 

Output

2

2

3

2

 

Module

Python module is a .py file consisting of python code. A python file can be referenced as a module. A module is used to logically organize your python code. Modules can contain definitions of functions, classes, and variables that can then be utilized in other python programs.

Creating a module

Creating a module is just like creating any other python file. So, let’s start by have following function definitions:

def add (n1, n2):

                return n1+n2

def sub(n1, n2):

                return n1-n2

def mul(nl, n2):

               return n1*n2

def div(n1, n2):’

             return n1/ n2

 

Importing a module

let’s create another file program.py so that we can use the module we just created, and then call the functions. This file needs to be in the same directory. The import statement is used to use the module as follow

import arithmetic

print(“Addition”,arithmetic.add(10, 20))

print(” Subtraction ” ,arithmetic.sub(20, 10))

print (“Multiplication.”, arithmetic.mul(20, 10))

print(“Division”, arithmetic.div(20, 10))

You can also use python’s from statement to import specific attributes from a module into the current python file. The from…import is used as follow

from arithmetic import *

print(“Addition:” add 10, 20))

print(“Subtraction:” sub(20, 10))

print(“Multiplication:”, mul(20, 10))

print(” Division.” div(20, 10))

Modules may be useful for more than one programming project, in that case it not good to keep a module in project directory. If you want to use a python module from a location other than the same directory where your main program is, you can do it as follow 2:

 Package

A package is basically a directory with python files and a file with the name _init__ .py. A directory must contain a file named __init__. py in order to consider it as a package. This fie can be left empty or we can put the initialization code for that package in this file. Python files other than __init__. py are treated as modules Packages may contain sub-packages and sub-modules. Creating a package The hierarchy of the packages we are going to create is as follow :

MyPackage                                               Top level package

         |……..__init__.py                              initialize the MyPackage

         |……..__MyModule.py                      A module

          |…….MySubPackage

                        |……__init__.py                              A sub package initialize the MySubPacsage

                         |……..MySubModule.py                   A sub module

The code inside MyModule module sub of MyPackage package is as follow:

def add(n1, n2):

                return n1 + n2

def sub(nl, n2)

               return n1-n2

The code inside MysubModule sub module of MySubPackage sub-package is as fellow:

def mul(n1, n2):

                 return n1 * n2

def div(n1, n2):

                return n1 /n2

 

Importing a package

Let’s create a file program.py so that we can use the package we just created, and then call the functions inside package module. This file needs to be in the same directory of top level package. The following statements how to import packages and such packages:

from MyPackage.MyMadule import add,sub

from MyPackage.MySubPackoage.MySubModule import mul, div

print(” Addition” ,add 10, 20)

print(“Subtraction”,sub(20, 10)

print(“Multiply”, mul(10, 20)

print(“Multiply” div(0, 20))

1 comment on “Local and Global Variable,Module,Package in Python

Leave a Reply

Your email address will not be published. Required fields are marked *