What is Namespace in Python with example?

It acts as a container for holding and managing the names used in a Python program.

Here’s an example that demonstrates namespaces in Python:

# Global namespace
global_var = 100

def my_function():
    # Local namespace (function namespace)
    local_var = 200
    print(global_var)  # Accessing global variable
    print(local_var)   # Accessing local variable

class MyClass:
    # Class namespace
    class_var = 300

    def __init__(self):
        # Instance namespace
        self.instance_var = 400

    def my_method(self):
        # Method namespace
        method_var = 500
        print(global_var)           # Accessing global variable
        print(self.class_var)       # Accessing class variable
        print(self.instance_var)    # Accessing instance variable
        print(method_var)           # Accessing local variable

# Accessing global variable
print(global_var)

# Creating objects and accessing variables/methods in the class namespace
obj1 = MyClass()
obj2 = MyClass()

print(obj1.class_var)           # Accessing class variable
print(obj1.instance_var)        # Accessing instance variable
obj1.my_method()                # Calling a method

print(obj2.class_var)           # Accessing class variable
print(obj2.instance_var)        # Accessing instance variable
obj2.my_method()                # Calling a method

# Accessing local variable outside its scope will result in an error
# print(local_var)  # NameError: name 'local_var' is not defined
Code language: Python (python)

In the example, there are different namespaces such as the global namespace, function namespace, class namespace, and instance namespace. Each namespace has its own set of variables that can be accessed within its scope.

What are the different types of namespaces in Python?

There are several types of namespaces that are used to organize and manage names (variables, functions, classes, etc.) within a program. The different types of namespaces in Python include:

Built-in Namespace:

  • This namespace contains names of built-in functions, exceptions, and attributes that are automatically available in Python without the need for any import statements.
  • Names such as print(), len(), TypeError, True, and False are part of the built-in namespace.

Global Namespace:

  • This namespace holds names defined at the top level of a module or script.
  • Names defined in the global namespace can be accessed from any part of the module or script.
  • Variables, functions, classes, and other objects defined at the top level are part of the global namespace.
global_var = 100  # Creates a variable in the global namespaceCode language: Python (python)

Local Namespace (Function Namespace):

  • This namespace is created when a function is called or executed.
  • It contains the names defined within the function.
  • Local variables, function parameters, and any other names defined within the function are part of the local namespace.
  • The local namespace is created and destroyed each time the function is called and returns.
def my_function():
    local_var = 200  # Creates a variable in the local namespace of my_functionCode language: Python (python)

Class Namespace:

  • This namespace holds the names defined within a class.
  • It includes class variables, methods, and nested classes.
  • Class namespaces are shared by all instances of the class.
  • Names defined within the class namespace are accessible through class objects and instance objects.
class MyClass:
    class_var = 300  # Creates a variable in the class namespace

    def my_method(self):
        # Method namespace
        method_var = 500  # Creates a variable in the method namespace
Code language: Python (python)

Instance Namespace:

  • This namespace is created for each instance of a class.
  • It holds the names specific to each instance, such as instance variables.
  • Instance namespaces are unique to each instance and separate from the class namespace.
obj = MyClass()  # Creates an instance of MyClass with its own instance namespaceCode language: Python (python)

It’s important to note that namespaces are implemented using dictionaries in Python, where the names are the keys and the objects are the values.

Namespaces provide a way to organize and manage names, avoiding naming conflicts and providing a mechanism for accessing the appropriate names within their respective scopes.

Read More;

  • I am a middle python software engineer with a bachelor's degree in Software Engineering from Kharkiv National Aerospace University. My expertise lies in Python, Django, Flask, Docker, REST API, Odoo development, relational databases, and web development. I am passionate about creating efficient and scalable software solutions that drive innovation in the industry.

    View all posts

Leave a Comment