How will you compare modules, classes and namespaces in Python?

Python allows you to save definitions to a file and then use them in a script or interactive instance of the interpreter. A module is a file that contains definitions that can be imported into other modules or the main module.

So, a Python module is nothing more than a package that contains reusable code. Modules are stored in a folder that contains a __init .py file. Modules can contain both functions and classes. The import keyword is used to import modules. A file containing Python commands and definitions is referred to as a module.

These files named .py which contain that contains Python code, such as, and the name of the module is an example. Modules are used to divide down huge applications into smaller, more manageable files. The prerequisites for using modules you should have Python 3 installed and a programming environment set up.

Modules in Python

Modules are essentially a group of flies containing classes and functions. You can use already existing modules or build custom modules in python. In the below example, you can understand how to create a simple user-defined module.


Let us look at an example to create a simple python module. Let us make a python file

def add(x, y):
   return (x+y)


Upon saving the above file, the following is done. A module named is created.

Namespaces in python

In Python, we can import definitions from one module into another or into the interactive interpreter. To accomplish this, we employ the import keyword. A namespace is a method of implementing scope. Each package, module, class, function, and method function in Python has its own "namespace" where variable names are resolved.

A namespace is created when a function, module, or package is evaluated (that is, when execution begins). Consider it a "evaluation context." When a function, for example, completes its execution, the namespace is removed. The variables have been removed. There is also a global namespace that is used if the name is not found in the local namespace.

There are four different sorts of namespaces in a Python program −

  • Built-In

  • Global

  • Enclosing

  • Local

These have varying life spans. Namespaces are created and deleted as needed during the course of a Python program's execution. There will typically be a lot of namespaces active at once.

Built-In Namespace

All of Python's built-in objects have names, which are all contained in the built-in namespace. When Python is running, these are always accessible.


For instance, when we perform the assignment a = 2, an is the name we give it to, and 2 is an object that is stored in memory. Through the built-in function id, we may obtain the address (in RAM) of some object (). Let's examine its application.

a = 2
print('id(2) =', id(2))
print('id(a) =', id(a))


The output obtained is as follows.

id(2) = 140378523304208
id(a) = 140378523304208

Global Namespace

Multiple namespaces may contain the same object name since each namespace maintains its own isolation from the others.


The below example, can be used to understand global namespace.

count = 10
def method():
   global count
   count = count + 1


The output obtained is as follows.


Local Namespace

A class, function, loop, or any other type of code block has a declared local namespace. A function or a block of code's defined names are specific to those areas. The variable names are only accessible within the function or block of code in which they are defined.


The function add in this case defines the variables num1, num2, and temp in the local namespace.

N1 = 20
N2 = 30
def add(num1, num2):
   temp = num1 + num2
   return temp


The output obtained is as follows.


Updated on: 11-May-2023


Kickstart Your Career

Get certified by completing the course

Get Started