What is difference between builtin and globals namespaces in Python?



In Python, namespaces are used to organize and manage names (e.g. variables, functions, etc.) in your code. There are several namespaces in Python, including built-in, global, and local namespaces. Here, we'll focus on the differences between the built-in and global namespaces.

Built-in namespace:

The built-in namespace in Python contains names that are built into the language and are available to all Python code by default. This includes names such as print(), len(), str(), etc.

Example

# Using a built-in function
print(len("Hello, world!"))

Output

13
 

Global namespace:

The global namespace in Python contains names that are defined at the global level, outside of any functions or classes. These names are available throughout your code, but they are not built into the language.

Example

# Defining a global variable
my_var = 42

def my_func():
    print(my_var)

my_func()  

Output

42
 

Accessing a built-in name from the global namespace:

When you use a built-in name in your code, Python first looks for it in the local namespace, then in the global namespace, and finally in the built-in namespace.

Example

# Overriding a built-in name
len = 42

# This will raise a TypeError
print(len("Hello, world!"))

Output

Traceback (most recent call last):
  File "/home/cg/root/44618/main.py", line 5, in 
    print(len("Hello, world!"))
TypeError: 'int' object is not callable
 

Accessing a global name from a function:

When you define a function in Python, it creates a new local namespace for that function. Names defined within the function are only available within that local namespace, unless they are explicitly marked as global.

Example

# Using a global variable within a function
my_var = 42
def my_func():
    global my_var
    return my_var
    # function code using the global variable
print(my_func()) 

Output

42
 

In Python, there are several namespaces that are used to store and manage variables and functions. Two of the most important namespaces are the built-in namespace and the global namespace. Here are some examples of the differences between these two namespaces:

Accessing built-in functions:

Example

# Using the built-in `print` function
print("Hello, world!")

# Accessing the `print` function from the built-in namespace
__builtins__.print("Hello, world!")

Output

Hello world!
Hello world!
 

The first example uses the print function directly, which is part of the built-in namespace. The second example accesses the same print function using the __builtins__ namespace.

Accessing built-in variables:

Example

# Using the built-in `True` variable
if True:
    print("This will be printed")

# Accessing the `True` variable from the built-in namespace
if __builtins__.True:
    print("This will also be printed")

Output

This will be printed
This also will be printed
 

The first example uses the True variable directly, which is part of the built-in namespace. The second example accesses the same True variable using the __builtins__ namespace.

Defining global variables:

Example

# Defining a global variable
x = 10
# Accessing the global variable
def foo():
    print(x)
foo() 

Output

10
 

The x variable is defined in the global namespace and can be accessed from within the foo function.

Defining local variables:

Example

# Defining a local variable
def foo():
    x = 10
    print(x)

foo()  

Output

10
 

The x variable is defined as a local variable within the foo function and is not accessible outside of the function.

Modifying global variables:

Example

# Modifying a global variable
x = 10
def foo():
    global x
    x = 20
foo()
print(x)  

Output

20
 

The global keyword is used to modify the x variable in the global namespace from within the foo function.

Overall, understanding the differences between the built-in and global namespaces in Python is important for managing and organizing your code. By using the examples above, you can customize your namespaces to meet your specific needs and ensure that your Python code is organized and easy to maintain.


Advertisements