Skip to main content
Log in

Mojo struct

TypedPythonObject

@register_passable struct TypedPythonObject[type_hint: StringLiteral]

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 (StringLiteral): The type name hint indicating the static type of this object.

Implemented traits

AnyType, SizedRaising, UnknownDestructibility

Methods

__init__

__init__(out self, *, owned unsafe_unchecked_from: PythonObject)

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.

__copyinit__

__copyinit__(out self, other: Self)

Copy an instance of this type.

Args:

  • other (Self): The value to copy.

__getitem__

__getitem__(self: TypedPythonObject["Tuple"], pos: Int) -> PythonObject

Get an element from this tuple.

Args:

  • pos (Int): 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.

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.