Mojo struct
PythonObject
A Python object.
Fieldsβ
- βpy_object (
PyObjectPtr
): A pointer to the underlying Python object.
Implemented traitsβ
AnyType
,
Boolable
,
CollectionElement
,
Copyable
,
EqualityComparable
,
Formattable
,
Hashable
,
ImplicitlyBoolable
,
Indexer
,
Intable
,
KeyElement
,
Movable
,
SizedRaising
,
Stringable
Methodsβ
__init__
β
__init__(inout self: Self)
Initialize the object with a None
value.
__init__(inout self: Self, *, other: Self)
Copy the object.
Args:
- βother (
Self
): The value to copy.
__init__(inout self: Self, ptr: PyObjectPtr)
Initialize the object with a PyObjectPtr
value.
Ownership of the reference will be assumed by PythonObject
.
Args:
- βptr (
PyObjectPtr
): ThePyObjectPtr
to take ownership of.
__init__(inout self: Self, none: None)
Initialize a none value object from a None
literal.
Args:
- βnone (
None
): None.
__init__(inout self: Self, none: NoneType)
Initialize a none value object from a None
literal.
Args:
- βnone (
NoneType
): None.
__init__(inout self: Self, integer: Int)
Initialize the object with an integer value.
Args:
- βinteger (
Int
): The integer value.
__init__(inout self: Self, float: SIMD[float64, 1])
Initialize the object with an floating-point value.
Args:
- βfloat (
SIMD[float64, 1]
): The float value.
__init__[dt: DType](inout self: Self, value: SIMD[dt, 1])
Initialize the object with a generic scalar value. If the scalar value type is bool, it is converted to a boolean. Otherwise, it is converted to the appropriate integer or floating point type.
Parameters:
- βdt (
DType
): The scalar value type.
Args:
- βvalue (
SIMD[dt, 1]
): The scalar value.
__init__(inout self: Self, value: Bool)
Initialize the object from a bool.
Args:
- βvalue (
Bool
): The boolean value.
__init__(inout self: Self, value: StringLiteral)
Initialize the object from a string literal.
Args:
- βvalue (
StringLiteral
): The string value.
__init__(inout self: Self, strref: StringRef)
Initialize the object from a string reference.
Args:
- βstrref (
StringRef
): The string reference.
__init__(inout self: Self, string: String)
Initialize the object from a string.
Args:
- βstring (
String
): The string value.
__init__[*Ts: Movable](inout self: Self, value: ListLiteral[Ts])
Initialize the object from a list literal.
Parameters:
- β*Ts (
Movable
): The list element types.
Args:
- βvalue (
ListLiteral[Ts]
): The list value.
__init__[*Ts: Movable](inout self: Self, value: Tuple[Ts])
Initialize the object from a tuple literal.
Parameters:
- β*Ts (
Movable
): The tuple element types.
Args:
- βvalue (
Tuple[Ts]
): The tuple value.
__init__(inout self: Self, value: Dict[PythonObject, PythonObject])
Initialize the object from a dictionary of PythonObjects.
Args:
- βvalue (
Dict[PythonObject, PythonObject]
): The dictionary value.
__copyinit__
β
__copyinit__(inout self: Self, existing: Self)
Copy the object.
This increments the underlying refcount of the existing object.
Args:
- βexisting (
Self
): The value to copy.
__del__
β
__del__(owned self: Self)
Destroy the object.
This decrements the underlying refcount of the pointed-to object.
__bool__
β
__bool__(self: Self) -> Bool
Evaluate the boolean value of the object.
Returns:
Whether the object evaluates as true.
__getitem__
β
__getitem__(self: Self, *args: Self) -> Self
Return the value for the given key or keys.
Args:
- β*args (
Self
): The key or keys to access on this object.
Returns:
The value corresponding to the given key for this object.
__setitem__
β
__setitem__(inout self: Self, *args: Self, *, value: Self)
Set the value with the given key or keys.
Args:
- β*args (
Self
): The key or keys to set on this object. - βvalue (
Self
): The value to set.
__neg__
β
__neg__(self: Self) -> Self
Negative.
Calls the underlying object's __neg__
method.
Returns:
The result of prefixing this object with a -
operator. For most numerical objects, this returns the negative.
__pos__
β
__pos__(self: Self) -> Self
Positive.
Calls the underlying object's __pos__
method.
Returns:
The result of prefixing this object with a +
operator. For most numerical objects, this does nothing.
__invert__
β
__invert__(self: Self) -> Self
Inversion.
Calls the underlying object's __invert__
method.
Returns:
The logical inverse of this object: a bitwise representation where all bits are flipped, from zero to one, and from one to zero.
__lt__
β
__lt__(self: Self, rhs: Self) -> Self
Less than comparator. This lexicographically compares strings and lists.
Args:
- βrhs (
Self
): Right hand value.
Returns:
True if the object is less than the right hard argument.
__le__
β
__le__(self: Self, rhs: Self) -> Self
Less than or equal to comparator. This lexicographically compares strings and lists.
Args:
- βrhs (
Self
): Right hand value.
Returns:
True if the object is less than or equal to the right hard argument.
__eq__
β
__eq__(self: Self, rhs: Self) -> Bool
Equality comparator. This compares the elements of strings and lists.
Args:
- βrhs (
Self
): Right hand value.
Returns:
True if the objects are equal.
__ne__
β
__ne__(self: Self, rhs: Self) -> Bool
Inequality comparator. This compares the elements of strings and lists.
Args:
- βrhs (
Self
): Right hand value.
Returns:
True if the objects are not equal.
__gt__
β
__gt__(self: Self, rhs: Self) -> Self
Greater than comparator. This lexicographically compares the elements of strings and lists.
Args:
- βrhs (
Self
): Right hand value.
Returns:
True if the left hand value is greater.
__ge__
β
__ge__(self: Self, rhs: Self) -> Self
Greater than or equal to comparator. This lexicographically compares the elements of strings and lists.
Args:
- βrhs (
Self
): Right hand value.
Returns:
True if the left hand value is greater than or equal to the right hand value.
__is__
β
__is__(self: Self, other: Self) -> Bool
Test if the PythonObject is the other
PythonObject, the same as x is y
in Python.
Args:
- βother (
Self
): The right-hand-side value in the comparison.
Returns:
True if they are the same object and False otherwise.
__isnot__
β
__isnot__(self: Self, other: Self) -> Bool
Test if the PythonObject is not the other
PythonObject, the same as x is not y
in Python.
Args:
- βother (
Self
): The right-hand-side value in the comparison.
Returns:
True if they are not the same object and False otherwise.
__add__
β
__add__(self: Self, rhs: Self) -> Self
Addition and concatenation.
Calls the underlying object's __add__
method.
Args:
- βrhs (
Self
): Right hand value.
Returns:
The sum or concatenated values.
__sub__
β
__sub__(self: Self, rhs: Self) -> Self
Subtraction.
Calls the underlying object's __sub__
method.
Args:
- βrhs (
Self
): Right hand value.
Returns:
The difference.
__mul__
β
__mul__(self: Self, rhs: Self) -> Self
Multiplication.
Calls the underlying object's __mul__
method.
Args:
- βrhs (
Self
): Right hand value.
Returns:
The product.
__truediv__
β
__truediv__(self: Self, rhs: Self) -> Self
Division.
Calls the underlying object's __truediv__
method.
Args:
- βrhs (
Self
): The right-hand-side value by which this object is divided.
Returns:
The result of dividing the right-hand-side value by this.
__floordiv__
β
__floordiv__(self: Self, rhs: Self) -> Self
Return the division of self and rhs rounded down to the nearest integer.
Calls the underlying object's __floordiv__
method.
Args:
- βrhs (
Self
): The right-hand-side value by which this object is divided.
Returns:
The result of dividing this by the right-hand-side value, modulo any remainder.
__mod__
β
__mod__(self: Self, rhs: Self) -> Self
Return the remainder of self divided by rhs.
Calls the underlying object's __mod__
method.
Args:
- βrhs (
Self
): The value to divide on.
Returns:
The remainder of dividing self by rhs.
__pow__
β
__pow__(self: Self, exp: Self) -> Self
Raises this object to the power of the given value.
Args:
- βexp (
Self
): The exponent.
Returns:
The result of raising this by the given exponent.
__lshift__
β
__lshift__(self: Self, rhs: Self) -> Self
Bitwise left shift.
Args:
- βrhs (
Self
): The right-hand-side value by which this object is bitwise shifted to the left.
Returns:
This value, shifted left by the given value.
__rshift__
β
__rshift__(self: Self, rhs: Self) -> Self
Bitwise right shift.
Args:
- βrhs (
Self
): The right-hand-side value by which this object is bitwise shifted to the right.
Returns:
This value, shifted right by the given value.
__and__
β
__and__(self: Self, rhs: Self) -> Self
Bitwise AND.
Args:
- βrhs (
Self
): The right-hand-side value with which this object is bitwise AND'ed.
Returns:
The bitwise AND result of this and the given value.
__or__
β
__or__(self: Self, rhs: Self) -> Self
Bitwise OR.
Args:
- βrhs (
Self
): The right-hand-side value with which this object is bitwise OR'ed.
Returns:
The bitwise OR result of this and the given value.
__xor__
β
__xor__(self: Self, rhs: Self) -> Self
Exclusive OR.
Args:
- βrhs (
Self
): The right-hand-side value with which this object is exclusive OR'ed.
Returns:
The exclusive OR result of this and the given value.
__radd__
β
__radd__(self: Self, lhs: Self) -> Self
Reverse addition and concatenation.
Calls the underlying object's __radd__
method.
Args:
- βlhs (
Self
): The left-hand-side value to which this object is added or concatenated.
Returns:
The sum.
__rsub__
β
__rsub__(self: Self, lhs: Self) -> Self
Reverse subtraction.
Calls the underlying object's __rsub__
method.
Args:
- βlhs (
Self
): The left-hand-side value from which this object is subtracted.
Returns:
The result of subtracting this from the given value.
__rmul__
β
__rmul__(self: Self, lhs: Self) -> Self
Reverse multiplication.
Calls the underlying object's __rmul__
method.
Args:
- βlhs (
Self
): The left-hand-side value that is multiplied by this object.
Returns:
The product of the multiplication.
__rtruediv__
β
__rtruediv__(self: Self, lhs: Self) -> Self
Reverse division.
Calls the underlying object's __rtruediv__
method.
Args:
- βlhs (
Self
): The left-hand-side value that is divided by this object.
Returns:
The result of dividing the given value by this.
__rfloordiv__
β
__rfloordiv__(self: Self, lhs: Self) -> Self
Reverse floor division.
Calls the underlying object's __rfloordiv__
method.
Args:
- βlhs (
Self
): The left-hand-side value that is divided by this object.
Returns:
The result of dividing the given value by this, modulo any remainder.
__rmod__
β
__rmod__(self: Self, lhs: Self) -> Self
Reverse modulo.
Calls the underlying object's __rmod__
method.
Args:
- βlhs (
Self
): The left-hand-side value that is divided by this object.
Returns:
The remainder from dividing the given value by this.
__rpow__
β
__rpow__(self: Self, lhs: Self) -> Self
Reverse power of.
Args:
- βlhs (
Self
): The number that is raised to the power of this object.
Returns:
The result of raising the given value by this exponent.
__rlshift__
β
__rlshift__(self: Self, lhs: Self) -> Self
Reverse bitwise left shift.
Args:
- βlhs (
Self
): The left-hand-side value that is bitwise shifted to the left by this object.
Returns:
The given value, shifted left by this.
__rrshift__
β
__rrshift__(self: Self, lhs: Self) -> Self
Reverse bitwise right shift.
Args:
- βlhs (
Self
): The left-hand-side value that is bitwise shifted to the right by this object.
Returns:
The given value, shifted right by this.
__rand__
β
__rand__(self: Self, lhs: Self) -> Self
Reverse bitwise and.
Args:
- βlhs (
Self
): The left-hand-side value that is bitwise AND'ed with this object.
Returns:
The bitwise AND result of the given value and this.
__ror__
β
__ror__(self: Self, lhs: Self) -> Self
Reverse bitwise OR.
Args:
- βlhs (
Self
): The left-hand-side value that is bitwise OR'ed with this object.
Returns:
The bitwise OR result of the given value and this.
__rxor__
β
__rxor__(self: Self, lhs: Self) -> Self
Reverse exclusive OR.
Args:
- βlhs (
Self
): The left-hand-side value that is exclusive OR'ed with this object.
Returns:
The exclusive OR result of the given value and this.
__iadd__
β
__iadd__(inout self: Self, rhs: Self)
Immediate addition and concatenation.
Args:
- βrhs (
Self
): The right-hand-side value that is added to this object.
__isub__
β
__isub__(inout self: Self, rhs: Self)
Immediate subtraction.
Args:
- βrhs (
Self
): The right-hand-side value that is subtracted from this object.
__imul__
β
__imul__(inout self: Self, rhs: Self)
In-place multiplication.
Calls the underlying object's __imul__
method.
Args:
- βrhs (
Self
): The right-hand-side value by which this object is multiplied.
__itruediv__
β
__itruediv__(inout self: Self, rhs: Self)
Immediate division.
Args:
- βrhs (
Self
): The value by which this object is divided.
__ifloordiv__
β
__ifloordiv__(inout self: Self, rhs: Self)
Immediate floor division.
Args:
- βrhs (
Self
): The value by which this object is divided.
__imod__
β
__imod__(inout self: Self, rhs: Self)
Immediate modulo.
Args:
- βrhs (
Self
): The right-hand-side value that is used to divide this object.
__ipow__
β
__ipow__(inout self: Self, rhs: Self)
Immediate power of.
Args:
- βrhs (
Self
): The exponent.
__ilshift__
β
__ilshift__(inout self: Self, rhs: Self)
Immediate bitwise left shift.
Args:
- βrhs (
Self
): The right-hand-side value by which this object is bitwise shifted to the left.
__irshift__
β
__irshift__(inout self: Self, rhs: Self)
Immediate bitwise right shift.
Args:
- βrhs (
Self
): The right-hand-side value by which this object is bitwise shifted to the right.
__iand__
β
__iand__(inout self: Self, rhs: Self)
Immediate bitwise AND.
Args:
- βrhs (
Self
): The right-hand-side value with which this object is bitwise AND'ed.
__ixor__
β
__ixor__(inout self: Self, rhs: Self)
Immediate exclusive OR.
Args:
- βrhs (
Self
): The right-hand-side value with which this object is exclusive OR'ed.
__ior__
β
__ior__(inout self: Self, rhs: Self)
Immediate bitwise OR.
Args:
- βrhs (
Self
): The right-hand-side value with which this object is bitwise OR'ed.
__iter__
β
__iter__(self: Self) -> _PyIter
Iterate over the object.
Returns:
An iterator object.
Raises:
If the object is not iterable.
__getattr__
β
__getattr__(self: Self, name: StringLiteral) -> Self
Return the value of the object attribute with the given name.
Args:
- βname (
StringLiteral
): The name of the object attribute to return.
Returns:
The value of the object attribute with the given name.
__setattr__
β
__setattr__(self: Self, name: StringLiteral, newValue: Self)
Set the given value for the object attribute with the given name.
Args:
- βname (
StringLiteral
): The name of the object attribute to set. - βnewValue (
Self
): The new value to be set for that attribute.
__as_bool__
β
__as_bool__(self: Self) -> Bool
Evaluate the boolean value of the object.
Returns:
Whether the object evaluates as true.
__len__
β
__len__(self: Self) -> Int
Returns the length of the object.
Returns:
The length of the object.
__hash__
β
__hash__(self: Self) -> UInt
Returns the length of the object.
Returns:
The length of the object.
__call__
β
__call__(self: Self, *args: Self, *, owned **kwargs: Self) -> Self
Call the underlying object as if it were a function.
Args:
- β*args (
Self
): Positional arguments to the function. - β**kwargs (
Self
): Keyword arguments to the function.
Returns:
The return value from the called object.
Raises:
If the function cannot be called for any reason.
to_float64
β
to_float64(self: Self) -> SIMD[float64, 1]
Returns a float representation of the object.
Returns:
A floating point value that represents this object.
__index__
β
__index__(self: Self) -> Int
Returns an index representation of the object.
Returns:
An index value that represents this object.
__int__
β
__int__(self: Self) -> Int
Returns an integral representation of the object.
Returns:
An integral value that represents this object.
unsafe_get_as_pointer
β
unsafe_get_as_pointer[type: DType](self: Self) -> UnsafePointer[SIMD[$0, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
Convert a Python-owned and managed pointer into a Mojo pointer.
Warning: converting from an integer to a pointer is unsafe! The compiler assumes the resulting pointer DOES NOT alias any Mojo-derived pointer. This is OK because the pointer originates from Python.
Parameters:
- βtype (
DType
): The desired DType of the pointer.
Returns:
An UnsafePointer
for the underlying Python data.
__str__
β
__str__(self: Self) -> String
Returns a string representation of the object.
Calls the underlying object's __str__
method.
Returns:
A string that represents this object.
format_to
β
format_to(self: Self, inout writer: Formatter)
Formats this Python object to the provided formatter.
Args:
- βwriter (
Formatter
): The formatter to write to.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
If you'd like to share more information, please report an issue on GitHub
π What went wrong?