object

Module

Defines the object type, which is used to represent untyped values.

These are Mojo built-ins, so you don’t need to import them.

Attr

A generic object’s attributes are set on construction, after which the attributes can be read and modified, but no attributes may be removed or added.

Fields:

  • key (StringLiteral): The name of the attribute.
  • value (object): The value of the attribute.

Functions:

__init__

static __init__(inout self: Self, key: StringLiteral, owned value: object)

Initializes the attribute with a key and value.

Args:

  • key (StringLiteral): The string literal key.
  • value (object): The object value of the attribute.

__del__

static __del__(owned self: Self)

object

Represents an object without a concrete type.

This is the type of arguments in def functions that do not have a type annotation, such as the type of x in def f(x): pass. A value of any type can be passed in as the x argument in this case, and so that value is used to construct this object type.

Aliases:

  • nullary_function = fn() raises -> object: Nullary function type.
  • unary_function = fn(object) raises -> object: Unary function type.
  • binary_function = fn(object, object) raises -> object: Binary function type.
  • ternary_function = fn(object, object, object) raises -> object: Ternary function type.

Functions:

__init__

static __init__(inout self: Self)

Initializes the object with a None value.

static __init__(inout self: Self, impl: _ObjectImpl)

Initializes the object with an implementation value. This is meant for internal use only.

Args:

  • impl (_ObjectImpl): The object implementation.

static __init__(inout self: Self, none: None)

Initializes a none value object from a None literal.

Args:

  • none (None): None.

static __init__(inout self: Self, value: Int)

Initializes the object with an integer value.

Args:

  • value (Int): The integer value.

static __init__(inout self: Self, value: FloatLiteral)

Initializes the object with an floating-point value.

Args:

  • value (FloatLiteral): The float value.

static __init__[dt: DType](inout self: Self, value: SIMD[dt, 1])

Initializes 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.

static __init__(inout self: Self, value: Bool)

Initializes the object from a bool.

Args:

  • value (Bool): The boolean value.

static __init__(inout self: Self, value: StringLiteral)

Initializes the object from a string literal.

Args:

  • value (StringLiteral): The string value.

static __init__(inout self: Self, value: StringRef)

Initializes the object from a string reference.

Args:

  • value (StringRef): The string value.

static __init__[*Ts: AnyType](inout self: Self, value: ListLiteral[Ts])

Initializes the object from a list literal.

Parameters:

  • Ts (*AnyType): The list element types.

Args:

  • value (ListLiteral[Ts]): The list value.

static __init__(inout self: Self, func: fn() raises -> object)

Initializes an object from a function that takes no arguments.

Args:

  • func (fn() raises -> object): The function.

static __init__(inout self: Self, func: fn(object) raises -> object)

Initializes an object from a function that takes one argument.

Args:

  • func (fn(object) raises -> object): The function.

static __init__(inout self: Self, func: fn(object, object) raises -> object)

Initializes an object from a function that takes two arguments.

Args:

  • func (fn(object, object) raises -> object): The function.

static __init__(inout self: Self, func: fn(object, object, object) raises -> object)

Initializes an object from a function that takes three arguments.

Args:

  • func (fn(object, object, object) raises -> object): The function.

static __init__(inout self: Self, *attrs: Attr)

Initializes the object with a sequence of zero or more attributes.

Args:

  • attrs (*Attr): Zero or more attributes.

__copyinit__

static __copyinit__(inout self: Self, existing: Self)

Copies the object. This clones the underlying string value and increases the refcount of lists or dictionaries.

Args:

  • existing (Self): The object to copy.

__moveinit__

static __moveinit__(inout self: Self, owned existing: Self)

Move the value of an object.

Args:

  • existing (Self): The object to move.

__del__

static __del__(owned self: Self)

Delete the object and release any owned memory.

__bool__

static __bool__(self: Self) -> Bool

Performs conversion to bool according to Python semantics. Integers and floats are true if they are non-zero, and strings and lists are true if they are non-empty.

Returns:

Whether the object is considered true.

__getitem__

static __getitem__(self: Self, i: Self) -> Self

Gets the i-th item from the object. This is only valid for strings, lists, and dictionaries.

Args:

  • i (Self): The string or list index, or dictionary key.

Returns:

The value at the index or key.

static __getitem__(self: Self, *i: Self) -> Self

Gets the i-th item from the object, where i is a tuple of indices.

Args:

  • i (*Self): A compound index.

Returns:

The value at the index.

__setitem__

static __setitem__(self: Self, i: Self, value: Self)

Sets the i-th item in the object. This is only valid for strings, lists, and dictionaries.

Args:

  • i (Self): The string or list index, or dictionary key.
  • value (Self): The value to set.

static __setitem__(self: Self, i: Self, j: Self, value: Self)

Sets the (i, j)-th element in the object.

FIXME: We need this because obj[i, j] = value will attempt to invoke this method with 3 arguments, and we can only have variadics as the last argument.

Args:

  • i (Self): The first index.
  • j (Self): The second index.
  • value (Self): The value to set.

__neg__

static __neg__(self: Self) -> Self

Negation operator. Only valid for bool, int, and float types. Negation on any bool value converts it to an integer.

Returns:

The negative of the current value.

__invert__

static __invert__(self: Self) -> Self

Invert value operator. This is only valid for bool and int values.

Returns:

The inverted value.

__lt__

static __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__

static __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__

static __eq__(self: Self, rhs: Self) -> Self

Equality comparator. This compares the elements of strings and lists.

Args:

  • rhs (Self): Right hand value.

Returns:

True if the objects are equal.

__ne__

static __ne__(self: Self, rhs: Self) -> Self

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__

static __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__

static __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.

__add__

static __add__(self: Self, rhs: Self) -> Self

Addition and concatenation operator. For arithmetic types, this function will compute the sum of the left and right hand values. For strings and lists, this function will concat the objects.

Args:

  • rhs (Self): Right hand value.

Returns:

The sum or concatenated values.

__sub__

static __sub__(self: Self, rhs: Self) -> Self

Subtraction operator. Valid only for arithmetic types.

Args:

  • rhs (Self): Right hand value.

Returns:

The difference.

__mul__

static __mul__(self: Self, rhs: Self) -> Self

Multiplication operator. Valid only for arithmetic types.

Args:

  • rhs (Self): Right hand value.

Returns:

The product.

__and__

static __and__(self: Self, rhs: Self) -> Self

Bool AND operator. If the left hand value is False, return the left-hand value.

Args:

  • rhs (Self): Right hand value.

Returns:

The current value if it is False.

__or__

static __or__(self: Self, rhs: Self) -> Self

Bool OR operator. If the left hand value is True, return the left-hand value.

Args:

  • rhs (Self): Right hand value.

Returns:

The current value if it is True.

__radd__

static __radd__(self: Self, lhs: Self) -> Self

Reverse addition or concatenation operator.

Args:

  • lhs (Self): Left hand value.

Returns:

The sum or concatenated value.

__rsub__

static __rsub__(self: Self, lhs: Self) -> Self

Reverse subtraction operator.

Args:

  • lhs (Self): Left hand value.

Returns:

The result of subtracting this from the left-hand-side value.

__rmul__

static __rmul__(self: Self, lhs: Self) -> Self

Reverse multiplication operator.

Args:

  • lhs (Self): Left hand value.

Returns:

The product.

__rand__

static __rand__(self: Self, lhs: Self) -> Self

Reverse AND operator.

Args:

  • lhs (Self): Left hand value.

Returns:

The bitwise AND of the left-hand-side value and this.

__ror__

static __ror__(self: Self, lhs: Self) -> Self

Reverse OR operator.

Args:

  • lhs (Self): Left hand value.

Returns:

The bitwise OR of the left-hand-side value and this.

__iadd__

static __iadd__(inout self: Self, rhs: Self)

In-place addition or concatenation operator.

Args:

  • rhs (Self): Right hand value.

__isub__

static __isub__(inout self: Self, rhs: Self)

In-place subtraction operator.

Args:

  • rhs (Self): Right hand value.

__imul__

static __imul__(inout self: Self, rhs: Self)

In-place multiplication operator.

Args:

  • rhs (Self): Right hand value.

__iand__

static __iand__(inout self: Self, rhs: Self)

In-place AND operator.

Args:

  • rhs (Self): Right hand value.

__ior__

static __ior__(inout self: Self, rhs: Self)

In-place OR operator.

Args:

  • rhs (Self): Right hand value.

append

static append(self: Self, value: Self)

Appends a value to the list.

Args:

  • value (Self): The value to append.

__len__

static __len__(self: Self) -> Int

Returns the “length” of the object. Only strings, lists, and dictionaries have lengths.

Returns:

The length of the string value or the number of elements in the list or dictionary value.

__getattr__

static __getattr__(self: Self, key: StringLiteral) -> Self

__setattr__

static __setattr__(inout self: Self, key: StringLiteral, value: Self)

__call__

static __call__(self: Self) -> Self

static __call__(self: Self, arg0: Self) -> Self

static __call__(self: Self, arg0: Self, arg1: Self) -> Self

static __call__(self: Self, arg0: Self, arg1: Self, arg2: Self) -> Self

print

static print(self: Self)

Prints the value of the object.