Compare Namespaces in Python and C++



Namespaces help in organizing code, managing the scope of variables and preventing naming conflicts. Python and C++ use namespaces, but they do so in different ways. Below is an overview of namespaces in both.

Namespaces in C++

In C++, namespaces are created using the keyword 'namespace'. They are mainly intended to organize code into logical groups and avoid name conflicts, particularly when working with multiple libraries.

Example

In the following example we are going to how to use a namespace by utilizing '::' to access functions within that namespace.

#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 space 1
   second_space::func(); // Calls the function from space 2
   return 0;
}

Output

Following is the output of the above code ?

Inside first_space
Inside second_space

Namespaces in Python

In Python, namespaces are created using modules and packages. A namespace is essentially a mapping that associates variable names (keys) with their corresponding objects (values). You can access the members of a namespace in Python by using the dot operator '(.)'.

Namespaces are created at runtime as needed and are deleted when they are no longer required.

Example

In this example, we are going to use the dot operator '.' to access functions within a module. Each module has its own namespace.

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

def my_second_function():
    print("Hello from my_second_function!")

# main.py
import my_module

my_module.my_first_function()  # Accessing function
my_module.my_second_function()  # Accessing another function

Output

Following is the output of the above code ?

Hello from my_first_function!
Hello from my_second_function!

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 includes all function and variable names from the modules used in the Python program, along with names in the local namespace.
  • Built-in Namespace ? This is the top-level namespace in Python, containing default names from the interpreter. It includes the Global Namespace, which contains the Local Namespace.

Key Differences

Following are some key differences between namespaces in Python and C++.

Feature C++ Namespaces Python Namespaces
Definition Defined using 'namespace' Created using modules
Access Method uses scope resolution operator '::' Uses the dot operator '.'
Creation Defined at compile-time Created dynamically during runtime
Lifetime Exists for the program's duration Exists while the module is loaded
Updated on: 2025-01-27T17:14:26+05:30

703 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements