[Numpy-discussion] Array literal

Sasha ndarray at mac.com
Fri Feb 10 16:09:01 EST 2006

Recent discussion of the numpy catenator (r_) made me realize that
Python syntax allows us to effectively implement an array literal.
>>> from numpy import r_ as a
>>> a[1:3,5:9]
array([1, 2, 5, 6, 7, 8])
>>> a[1, 2, 5, 6, 7, 8]
array([1, 2, 5, 6, 7, 8])

One can think of a[1, 2, 5, 6, 7, 8] as an array literal.  To me it
looks very "pythonic": [...] already has a meaning of list literal and
python uses single-letter modifier in string literals to denote raw
strings.  In other words a[...] is to [...] what r"..." is to "...". 
The catenator can probably be generalized to cover all use cases of
the "array" constructor.  For example:

a(shape=(2,3))[1:3,5:9] may return array([[1,2,5],[6,7,8]])
a(shape=(2,3))[1] may return ones((2,3))
a(shape=(2,3))[...] may return empty((2,3))
a(shape=(2,3))[1, 2, ...] may return array([[1,2,1],[2,1,2]])

dtype and other array(...) arguments can be passed similarly to shape
above.  If this syntax proves successful, ndarray repr may be changed
to return "a[...]" instead of "array([...])" and thus make new users
immediately aware of this way to represent arrays.

More information about the NumPy-Discussion mailing list