> For the complete documentation index, see [llms.txt](https://a7246c5516ab4c80cdfe21ca2be3e40c.gitbook.io/python-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://a7246c5516ab4c80cdfe21ca2be3e40c.gitbook.io/python-docs/builtins.md).

# 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:

```python
# 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://a7246c5516ab4c80cdfe21ca2be3e40c.gitbook.io/python-docs/builtins.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
