dtlussier dtlussier - 15 days ago 9
Python Question

Terminology: Python and Numpy - `iterable` versus `array_like`

What is the difference between an

iterable
and an
array_like
object in Python programs which use
Numpy
?

Both
iterable
and
array_like
are often seen in Python documentation and they share some similar properties.

I understand that in this context an
array_like
object should support
Numpy
type operations like broadcasting, however
Numpy
arrays area also iterable. Is it correct to say that
array_like
is an extension (or super-set?) of
iterable
?

Answer

The term "array-like" is indeed only used in NumPy and refers to anything that can be passed as first parameter to numpy.array() to create an array.

The term "iterable" is standard python terminology and refers to anything that can be iterated over (for example using for x in iterable).

All array-like objects are iterable, but not all iterables are array-like -- for example you can't construct a NumPy array from a generator expression using numpy.array(). (You would have to use numpy.fromiter() instead. Nonetheless, a generator expression isn't an "array-like" in the terminology of the NumPy documentation.)

Comments