Mojo struct
Python
Provides methods that help you use Python code in Mojo.
Fieldsβ
- βimpl (
_PythonInterfaceImpl
): The underlying implementation of Mojo's Python interface.
Implemented traitsβ
AnyType
Methodsβ
__init__
β
__init__(inout self: Self)
Default constructor.
__copyinit__
β
__copyinit__(inout self: Self, existing: Self)
Copy constructor.
Args:
- βexisting (
Self
): The existing instance to copy from.
eval
β
eval(inout self: Self, code: StringRef) -> Bool
Executes the given Python code.
Args:
- βcode (
StringRef
): The python code to execute.
Returns:
True
if the code executed successfully or False
if the code raised an exception.
evaluate
β
static evaluate(expr: StringRef, file: Bool = 0, name: StringRef = "__main__") -> PythonObject
Executes the given Python code.
Args:
- βexpr (
StringRef
): The Python expression to evaluate. - βfile (
Bool
): Evaluate as a file and return the module. - βname (
StringRef
): The name of the module (most relevant iffile
is True).
Returns:
PythonObject
containing the result of the evaluation.
add_to_path
β
static add_to_path(dir_path: String)
Adds a directory to the Python path.
This might be necessary to import a Python module via import_module()
.
For example:
from python import Python
# Specify path to `mypython.py` module
Python.add_to_path("path/to/module")
var mypython = Python.import_module("mypython")
var c = mypython.my_algorithm(2, 3)
from python import Python
# Specify path to `mypython.py` module
Python.add_to_path("path/to/module")
var mypython = Python.import_module("mypython")
var c = mypython.my_algorithm(2, 3)
Args:
- βdir_path (
String
): The path to a Python module you want to import.
import_module
β
static import_module(module: StringRef) -> PythonObject
Imports a Python module.
This provides you with a module object you can use just like you would in Python. For example:
from python import Python
# This is equivalent to Python's `import numpy as np`
var np = Python.import_module("numpy")
a = np.array([1, 2, 3])
from python import Python
# This is equivalent to Python's `import numpy as np`
var np = Python.import_module("numpy")
a = np.array([1, 2, 3])
Args:
- βmodule (
StringRef
): The Python module name. This module must be visible from the list of available Python paths (you might need to add the module's path withadd_to_path()
).
Returns:
The Python module.
dict
β
static dict() -> PythonObject
Construct an empty Python dictionary.
Returns:
The constructed empty Python dictionary.
list
β
static list() -> PythonObject
Construct an empty Python list.
Returns:
The constructed empty Python list.
__str__
β
__str__(inout self: Self, str_obj: PythonObject) -> StringRef
Return a string representing the given Python object.
Args:
- βstr_obj (
PythonObject
): The Python object.
Returns:
Mojo string representing the given Python object.
throw_python_exception_if_error_state
β
static throw_python_exception_if_error_state(inout cpython: CPython)
Raise an exception if CPython interpreter is in an error state.
Args:
- βcpython (
CPython
): The cpython instance we wish to error check.
is_type
β
static is_type(x: PythonObject, y: PythonObject) -> Bool
Test if the x
object is the y
object, the same as x is y
in Python.
Args:
- βx (
PythonObject
): The left-hand-side value in the comparison. - βy (
PythonObject
): The right-hand-side type value in the comparison.
Returns:
True if x
and y
are the same object and False otherwise.
type
β
static type(obj: PythonObject) -> PythonObject
Return Type of this PythonObject.
Args:
- βobj (
PythonObject
): PythonObject we want the type of.
Returns:
A PythonObject that holds the type object.
none
β
static none() -> PythonObject
Get a PythonObject
representing None
.
Returns:
PythonObject
representing None
.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
π What went wrong?