Named Array Tuples

rlpyt.utils.collections.namedarraytuple(typename, field_names, return_namedtuple_cls=False, classname_suffix=False)

Returns a new subclass of a namedtuple which exposes indexing / slicing reads and writes applied to all contained objects, which must share indexing (__getitem__) behavior (e.g. numpy arrays or torch tensors).

(Code follows pattern of collections.namedtuple.)

>>> PointsCls = namedarraytuple('Points', ['x', 'y'])
>>> p = PointsCls(np.array([0, 1]), y=np.array([10, 11]))
>>> p
Points(x=array([0, 1]), y=array([10, 11]))
>>> p.x                         # fields accessible by name
array([0, 1])
>>> p[0]                        # get location across all fields
Points(x=0, y=10)               # (location can be index or slice)
>>> p.get(0)                    # regular tuple-indexing into field
array([0, 1])
>>> x, y = p                    # unpack like a regular tuple
>>> x
array([0, 1])
>>> p[1] = 2                    # assign value to location of all fields
>>> p
Points(x=array([0, 2]), y=array([10, 2]))
>>> p[1] = PointsCls(3, 30)     # assign to location field-by-field
>>> p
Points(x=array([0, 3]), y=array([10, 30]))
>>> 'x' in p                    # check field name instead of object
class rlpyt.utils.collections.DocExampleNat(field_1, field_2)

Checks presence of field name (unlike tuple; like dict).


Return a new DocExampleNat instance containing the selected index or slice from each field.

__setitem__(loc, value)

If input value is the same named[array]tuple type, iterate through its fields and assign values into selected index or slice of corresponding field. Else, assign whole of value to selected index or slice of all fields. Ignore fields that are both None.


Retrieve value as if indexing into regular tuple.


Iterate ordered (field_name, value) pairs (like OrderedDict).


Heuristic, might be spoofed. Returns False if obj is namedarraytuple class.


Heuristic, might be spoofed. Returns False if obj is namedtuple class.


Heuristic, might be spoofed. Returns False if obj is namedarraytuple.


Heuristic, might be spoofed. Returns False if obj is namedtuple.

rlpyt.utils.collections.namedarraytuple_like(namedtuple_or_class, classname_suffix=False)

Returns namedarraytuple class with same name and fields as input namedtuple or namedarraytuple instance or class. If input is namedarraytuple instance or class, the same class is returned directly. Input can also be from the new Schema format, instances of the four: Named[Array]Tuple[Schema].

Alternative Implementation

Classes for creating objects which closely follow the interfaces for namedtuple and namedarraytuple types and instances, except without defining a new class for each type. (May be easier to use with regards to pickling under spawn, or dynamically creating types, by avoiding module-level definitions.

class rlpyt.utils.collections.NamedTupleSchema(typename, fields)

Instances of this class act like a type returned by namedtuple().

__call__(*args, **kwargs)

Allows instances to act like namedtuple constructors.


Allows instances to act like namedtuple constructors.

class rlpyt.utils.collections.NamedTuple

Bases: tuple

Instances of this class act like instances of namedtuple types, but this same class is used for all namedtuple-like types created. Unlike true namedtuples, this mock avoids defining a new class for each configuration of typename and field names. Methods from namedtuple source are copied here.

Implementation differences from namedtuple:

  • The individual fields don’t show up in dir(obj), but they do still show up as hasattr(obj, field) => True, because of __getattr__().
  • These objects have a __dict__ (by ommitting __slots__ = ()), intended to hold only the typename and list of field names, which are now instance attributes instead of class attributes.
  • Since property(itemgetter(i)) only works on classes, __getattr__() is modified instead to look for field names.
  • Attempts to enforce call signatures are included, might not exactly match.

Look in _fields when name is not in dir(self).


Make a new object of same typename and fields from a sequence or iterable.


Return a new object of same typename and fields, replacing specified fields with new values.


Return an ordered dictionary mapping field names to their values.

class rlpyt.utils.collections.NamedArrayTupleSchema(*args, **kwargs)

Bases: rlpyt.utils.collections.NamedTupleSchema

Instances of this class act like a type returned by rlpyt’s namedarraytuple().

class rlpyt.utils.collections.NamedArrayTuple

Bases: rlpyt.utils.collections.NamedTuple


Returns a NamedArrayTupleSchema instance with the same name and fields as input, which can be a class or instance of namedtuple or namedarraytuple, or an instance of NamedTupleSchema, NamedTuple, NamedArrayTupleSchema, or NamedArrayTuple.