Skip to main content

Mojo module

bindings

Provides infrastructure for creating Python bindings to Mojo code.

This module implements the core machinery for exposing Mojo functions and types to Python through CPython's C API. It includes builder types for constructing Python modules and type objects, wrapper functions for converting between Mojo and Python calling conventions, and utilities for argument validation and type conversion. This enables seamless bidirectional interoperability between Mojo and Python code.

comptime values

GenericPyFunction

comptime GenericPyFunction = Variant[PyFunctionRaising, PyFunctionWithKeywordsRaising]

A variant type that can hold either a PyFunctionRaising or PyFunctionWithKeywordsRaising.

MOJO_PYTHON_TYPE_OBJECTS

comptime MOJO_PYTHON_TYPE_OBJECTS = _Global["MOJO_PYTHON_TYPE_OBJECTS", __init__[StaticString, PythonObject, default_hasher]]

Mapping of Mojo type identifiers to unique PyTypeObject* binding that Mojo type to this CPython interpreter instance.

PyFunctionRaising

comptime PyFunctionRaising = fn(mut PythonObject, mut PythonObject) raises -> PythonObject

The generic function type for raising Python bindings.

The first argument is the self object, and the second argument is a tuple of the positional arguments. These functions always return a Python object (could be a None object).

PyFunctionWithKeywordsRaising

comptime PyFunctionWithKeywordsRaising = fn(mut PythonObject, mut PythonObject, mut PythonObject) raises -> PythonObject

The generic function type for raising Python bindings with keyword arguments.

The first argument is the self object, the second argument is a tuple of the positional arguments, and the third argument is a dictionary of the keyword arguments.

Structs

Functions

Was this page helpful?