builtins

What is the builtins Module?

The builtins module is a special module in Python that provides access to all the built-in functions, variables, and exceptions that are available in the Python language. Built-in functions are functions that are available to use in any Python program without having to import them from a separate module.

Why Use the builtins Module?

In most cases, you won't need to use the builtins module directly. Python automatically imports the builtins module into every program, so you can use built-in functions like print() and len() without having to explicitly import them.

However, there are a few cases where you might want to use the builtins module directly. For example, you might want to use the builtins module to get a list of all the built-in functions, or to access a built-in function that has the same name as a variable or function in your program.

Examples

Here are a few examples of how to use the builtins module:

# Get a list of all the built-in functions
print(dir(builtins))

# Access a built-in function that has the same name as a variable or function in your program
x = 10

def len(x):
    return x + 1

print(len(x))  # This will call the built-in `len()` function, not the custom `len()` function

Real-World Applications

The builtins module can be used in a variety of real-world applications, such as:

  • Introspection: The builtins module can be used to get information about the Python interpreter and the running program. For example, you can use the builtins.dir() function to get a list of all the objects that are available in the current scope.

  • Customization: The builtins module can be used to customize the behavior of built-in functions. For example, you can use the builtins.open() function to open a file in a specific mode.

  • Error handling: The builtins module provides access to a number of built-in exceptions, which can be used to handle errors in your program. For example, you can use the builtins.ValueError exception to handle errors that occur when you try to convert a string to a number.


Built-in Objects and Functions

The builtins module provides access to Python's built-in objects and functions. These are the core elements of the language, such as lists, dictionaries, numbers, and common functions like print and input.

Accessing Built-in Functions

Normally, you don't need to import the builtins module to use these functions and objects. They are automatically available in your Python code. For example, you can use print() to output text:

print("Hello, world!")

Using Built-in and Custom Objects with the Same Name

Sometimes, you may want to define a custom object with the same name as a built-in object. For example, you could define a function called open() that wraps the built-in open() function and adds additional functionality.

To avoid confusion, you can use the builtins module to access the built-in function while using your custom function:

import builtins

def open(path):
    f = builtins.open(path, 'r')  # Access the built-in open function
    return UpperCaser(f)

class UpperCaser:
    '''Wrapper around a file that converts output to uppercase.'''
    def __init__(self, f):
        self._f = f

    def read(self, count=-1):
        return self._f.read(count).upper()

In this example, the custom open() function creates an UpperCaser object that reads from the built-in open() function and converts the output to uppercase.

Access to Built-ins as __builtins__

As an implementation detail, Python makes the __builtins__ variable available as part of the globals for most modules. This variable is usually assigned to the builtins module itself or its __dict__ attribute.

However, this is implementation-specific and not guaranteed by the Python language.

Applications in Real World

The builtins module is used in various scenarios:

  • Wrapping existing functions with custom functionality, such as the UpperCaser example.

  • Accessing specific built-in functions or objects in特殊 circumstances.

  • Creating custom versions of built-in objects for specific purposes.