#### 3.2.3 Sequence protocol

An object of type `Robj` supports (partially at the moment, slices are not supported yet) the sequence protocol. You can retrieve or set the n-th item of a `Robj` object, and you can take its length with the usual Python function `len`.

Every R object is a vector, so this protocol can be applied to any `Robj` object; although it can raise an exception when an index is out of bounds. Note that in this case, the exception is `IndexError` instead of `RException`; this is done to allow a `for` loop to iterate over a `Robj` object.

The return values of the sequence functions are converted to Python according to the default mode. If the default mode is set to ‘`NO_DEFAULT`’, the sequence functions use the ‘`PROC_MODE`’ conversion mode.

    >>> r.seq.local_mode(NO_CONVERSION)
    >>> a = r.seq(3, 5)
    >>> a[0]
    3
    >>> a[2]
    5
    >>> a[-1]
    5
    >>> a[4]
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IndexError: R object index out of range
    >>>
    >>> for i in a:
    ...     print i
    ...
    3
    4
    5

The behavior of the setting of items is different from that of Python, mainly, when you try to set an item out of bounds. Remember, in these cases, that the setting is done via R functions, which have different semantic from the Python sequence functions.

    (following the previous example)
    >>> b = r.seq(1, 3)
    >>> dummy = r.print_(b)
    [1] 1 2 3
    >>> b[0] = -1
    >>> dummy = r.print_(b)
    [1] -1  2  3
    >>> b[6] = 4
    >>> dummy = r.print_(b)
    [1] -1  2  3 NA NA NA  4

Also, be careful with the different index convention between Python and R: in Python, indices start at 0; in R, they start at 1.

    (following the previous example)
    >>> a[0]
    3
    >>> r['['](a, 1)
    3

Function `len` can also be applied to any `Robj` object:

    (following the previous example)
    >>> len(a)
    3
    >>> len(r.seq)
    1
    >>> len(r.pi)
    1