How will you compare namespaces in Python and C++?


C++ namespaces are explicitly defined blocks that help in avoiding name conflicts. Python namespaces too serve the same purpose of managing scope and name conflicts, but they are dynamically created using modules and packages.

Read this tutorial to understand how namespaces are treated in C++ and Python.

Namespaces in C++

In C++, namespaces are defined using the namespace keyword. Namespaces are primarily used to organize the code into logical groups and prevent name conflicts that can occur especially when your code base includes multiple libraries.

Example

Take a look at the following example −

#include <iostream>
using namespace std;

// first namespace
namespace first_space {
   void func() {
      cout << "Inside first_space" << endl;
   }
}

// second namespace
namespace second_space {
   void func() {
      cout << "Inside second_space" << endl;
   }
}

int main () {
   first_space::func();		// Calls the function from the first namespace
   second_space::func();	// Calls the function from the second namespace
   return 0;
}

In C++, if you need to use a namespace, then you must define it explicitly. You need to use the scope resolution operator ( : : ) to access the members of a namespace in C++.

Namespaces in Python

In Python, namespaces are implemented using modules and packages. A namespace in Python is essentially a mapping from variable names (keys) to their corresponding objects (values).

You can use the dot operator (.) to access the members of a namespace in Python. Take a look at the following example −

Example

# my_module.py
def my_first_function():
   print ("Hello World!")

def my_second_function():
   print ("This is the second function in the module")

# main.py
import my_module
result = my_module.my_function()

You can import specific names from a module to avoid using the module name each time −

from my_module import my_first_function
result = my_first_function()

Note − A Python statement can access variables in a local namespace and in the global namespace. If a local and a global variable have the same name, the local variable shadows the global variable.

Types of Namespaces

Following are the three types of namespaces −

  • Local Namespace − All the names of the functions and variables declared by a program are held in this namespace. This namespace exists as long as the program runs.
  • Global Namespace − This namespace holds all the names of functions and other variables that are included in the modules being used in the Python program. It includes all the names that are part of the Local namespace.
  • Built-in Namespace − This is the highest level of namespace which is available with default names available as part of the Python interpreter that is loaded as the programing environment. It contains the Global Namespace which in turn contains the local namespace.

In Python, namespaces are created in runtime as and when necessary. Also, namespaces are deleted when they are no longer required.

Updated on: 11-Jul-2024

444 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements