Python namespace tutorial

In this Python tutorial, we will learn about namespace in python. Also, we will check:

  • What is a Namespace in Python?
  • Python Local namespace
  • Global namespace
  • Built-in namespace
  • Python namespace class
  • Python namespace import
  • Python namespace to the dictionary
  • Python namespace and scope

Introduction to Python namespace

  • The name means the name of the variable, space is about the location from where the variable is accessed.
  • Namespace represents a memory block.
  • The namespace is a system that uses unique names for each and every object in the program. It is also a type of mapping from name to objects.

You may like Python copy file (Examples).

Local Namespace in Python

  • The Python local namespace contains local names inside the function.
  • It is created when the function is called and lasts until the function is returned.

In this example, I have defined a function vehicle, and the car is the local namespace that is inside the function vehicle.

Example:

def vehicle():
    car = "Baleno"
    print(car)
vehicle()

Below screenshot shows the output:

Python Local Namespace
Local Namespace

Python Global Namespace

  • The Python global namespace is created when modules are imported and it lasts up to the module ends.
  • It contains all the names of modules that are imported into the project.
  • In this example, I have defined function vehicle namespace, Bicycle namespace is defined outside the function() vehicle, so the Bicycle = “hero” is a global namespace.
  • It can also be printed inside the function.

Example:

Bicycle = "hero"
def vehicle():
    car = "Baleno"
    print("car:",car)
    print("Inside the function",Bicycle)
print("Bicycle:",Bicycle)
vehicle()

Below screenshot shows the output:

Python Global Namespace
Python Global Namespace

Built-in Namespace in Python

  • The Built-in namespace in Python is created when the python interpreter is started and exists until the interpreter runs.
  • This namespace covers the built-in functions, a built-in namespace contains the built-in functions like print(), open(), close().

In this example, I am not defining any function print is a built-in namespace in python.

Example:

print("vehicle")

Below screenshot shows the output:

Built-in Namespace
Built-in Namespace
Namespace = {"NameA":objectA, "NameB":objectB}

Python namespace to dictionary

Here, we can see how to convert argparse.namespace() object into a dictionary in python. The argparse module is easy to write a command-line interface, to import argparse module.

Syntax:

import argparse

In this example, I have imported argparse module, item() returns all the key-value pairs the variables are converted to the dictionary the namespace variables are converted into a dictionary using converted_dict = vars(namespace).

Example:

import argparse
namespace = argparse.Namespace()
namespace.item = "chocolate"
namespace.chocolate = ["5star", "diarymilk", "kitkat"]
print(namespace)
converted_dict = vars(namespace)
print(converted_dict)

Below screenshot shows the output:

Python namespace to dictionary
Python namespace to dictionary

Python namespace class

  • Here, we can see the namespace class in python. Classes have a local namespace, so that only they can access. Classes have their own namespace also.
  • In this example, class Chocolate is an initialized, class that has a special method called _init_().
  • The init() is customized to the specific initial state. **kwargs is a keyword argument that allows the variable length of keywords to function.
  • self.__dict __.update allows accessing to attributes and items. args = Chocolate(a=’kitkat’, b=’5star’) returns the result to print.

Example:

class Chocolate:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)
args = Chocolate(a='kitkat', b='5star')
print (args.a, args.b)

Below screenshots shows the output:

In this output, we can see the attributes assigned to args.

Python namespace class
Python namespace class

Python namespace import

Now, we can see the different types of import statements in python.

  • The import statement
  • The from statement
  • The from import* statement

The import statement

To get access to code from modules by importing files using import.

Syntax:

import module1[, module2[,---- moduleN]

The from statement

The from statement is used to import specific attributes from the module into the present namespace.

Syntax:

from modulename import name1[, name2[,---- nameN]]

Example:

from math import pi
from math import pi
print( "pi", pi)
The from statement
The from statement

The from import* statement

Here, from import* is used to import all the names from the module into present name space .

Syntax:

from modulename import *

Here, we can import any module name.

Python namespace and scope

When only a variable is available from inside the function region that is called scope. In this example, variable multiplication is not available for outside functions it’s only available for the inside function.

Example:

def number():
  num = 14
  print(num)

number()

Below screenshots shows the output:

We can see the correct output because the print statement is inside the function.

Python namespace and scope
Python namespace and scope

Here, we can see if we write the print statement outside the function then we will get an error. So, we have to follow the print statement inside the function in order to get the correct output.

Python namespace and scope
Python namespace and scope

You may like the following python tutorials:

Here, we learned about Python namespace.

  • What is a Python Namespace?
  • Python Local Namespace
  • Global Namespace in Python
  • Built-in Namespace in Python
  • Python namespace to dictionary
  • Python namespace class
  • Python namespace import
  • Python namespace and scope