Mojo struct
TypedPythonObject
@register_passable
struct TypedPythonObject[type_hint: StringSlice[StaticConstantOrigin]]
A wrapper around PythonObject
that indicates the type of the contained object.
The PythonObject structure is entirely dynamically typed. This type provides a weak layer of optional static typing.
Parameters
- type_hint (
StringSlice[StaticConstantOrigin]
): The type name hint indicating the static type of this object.
Implemented traits
AnyType
,
PythonConvertible
,
SizedRaising
,
UnknownDestructibility
Methods
__init__
__init__(*, owned unsafe_unchecked_from: PythonObject) -> Self
Construct a TypedPythonObject without any validation that the given object is of the specified hinted type.
Args:
- unsafe_unchecked_from (
PythonObject
): The PythonObject to construct from. This will not be type checked.
__init__(out self: TypedPythonObject[__init__[__mlir_type.!kgen.string]("Module")], name: StringSlice[StaticConstantOrigin])
Construct a Python module with the given name.
Args:
- name (
StringSlice[StaticConstantOrigin]
): The name of the module.
Raises:
If the module creation fails.
__copyinit__
__copyinit__(other: Self) -> Self
Copy an instance of this type.
Args:
- other (
Self
): The value to copy.
__getitem__
__getitem__[I: Index](self: TypedPythonObject[__init__[__mlir_type.!kgen.string]("Tuple")], pos: I) -> PythonObject
Get an element from this tuple.
Parameters:
- I (
Index
): A type that can be used as an index.
Args:
- pos (
I
): The tuple element position to retrieve.
Returns:
The value of the tuple element at the specified position.
__len__
__len__(self) -> Int
Returns the length of the object.
Returns:
The length of the object.
to_python_object
to_python_object(self) -> PythonObject
Convert the TypedPythonObject to a PythonObject.
Returns:
A PythonObject representing the value.
unsafe_as_py_object_ptr
unsafe_as_py_object_ptr(self) -> PyObjectPtr
Get the underlying PyObject pointer.
Safety:
Use-after-free: The caller must take care that self
outlives the
usage of the pointer returned by this function.
Returns:
The underlying PyObject pointer.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!