summaryrefslogtreecommitdiff
path: root/docs/library
diff options
context:
space:
mode:
authorHoward Lovatt <howard.lovatt@gmail.com>2020-10-10 08:48:41 +1100
committerDamien George <damien@micropython.org>2022-11-15 18:21:58 +1100
commitd2f8127258d91e4af5f5880e3519fc6d5cf879d9 (patch)
tree16af90e98657058c2314864e54475911eb029d80 /docs/library
parent40a3aa709c874f1eb3cabd6d8a393ea8042d976c (diff)
docs/library/array: Add docs for dunder methods.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'docs/library')
-rw-r--r--docs/library/array.rst52
1 files changed, 52 insertions, 0 deletions
diff --git a/docs/library/array.rst b/docs/library/array.rst
index 88d6d2263..f94cece2b 100644
--- a/docs/library/array.rst
+++ b/docs/library/array.rst
@@ -27,3 +27,55 @@ Classes
Append new elements as contained in *iterable* to the end of
array, growing it.
+
+ .. method:: __getitem__(index)
+
+ Indexed read of the array, called as ``a[index]`` (where ``a`` is an ``array``).
+ Returns a value if *index* is an ``int`` and an ``array`` if *index* is a slice.
+ Negative indices count from the end and ``IndexError`` is thrown if the index is
+ out of range.
+
+ **Note:** ``__getitem__`` cannot be called directly (``a.__getitem__(index)`` fails) and
+ is not present in ``__dict__``, however ``a[index]`` does work.
+
+ .. method:: __setitem__(index, value)
+
+ Indexed write into the array, called as ``a[index] = value`` (where ``a`` is an ``array``).
+ ``value`` is a single value if *index* is an ``int`` and an ``array`` if *index* is a slice.
+ Negative indices count from the end and ``IndexError`` is thrown if the index is out of range.
+
+ **Note:** ``__setitem__`` cannot be called directly (``a.__setitem__(index, value)`` fails) and
+ is not present in ``__dict__``, however ``a[index] = value`` does work.
+
+ .. method:: __len__()
+
+ Returns the number of items in the array, called as ``len(a)`` (where ``a`` is an ``array``).
+
+ **Note:** ``__len__`` cannot be called directly (``a.__len__()`` fails) and the
+ method is not present in ``__dict__``, however ``len(a)`` does work.
+
+ .. method:: __add__(other)
+
+ Return a new ``array`` that is the concatenation of the array with *other*, called as
+ ``a + other`` (where ``a`` and *other* are both ``arrays``).
+
+ **Note:** ``__add__`` cannot be called directly (``a.__add__(other)`` fails) and
+ is not present in ``__dict__``, however ``a + other`` does work.
+
+ .. method:: __iadd__(other)
+
+ Concatenates the array with *other* in-place, called as ``a += other`` (where ``a`` and *other*
+ are both ``arrays``). Equivalent to ``extend(other)``.
+
+ **Note:** ``__iadd__`` cannot be called directly (``a.__iadd__(other)`` fails) and
+ is not present in ``__dict__``, however ``a += other`` does work.
+
+ .. method:: __repr__()
+
+ Returns the string representation of the array, called as ``str(a)`` or ``repr(a)```
+ (where ``a`` is an ``array``). Returns the string ``"array(<type>, [<elements>])"``,
+ where ``<type>`` is the type code letter for the array and ``<elements>`` is a comma
+ seperated list of the elements of the array.
+
+ **Note:** ``__repr__`` cannot be called directly (``a.__repr__()`` fails) and
+ is not present in ``__dict__``, however ``str(a)`` and ``repr(a)`` both work.