Strange range

Marko Rauhamaa marko at pacujo.net
Sat Apr 2 06:48:20 EDT 2016


Steven D'Aprano <steve at pearwood.info>:

> On Sat, 2 Apr 2016 07:14 am, Marko Rauhamaa wrote:
>> (Somehow, the difference between iterables and iterators is analogous
>> with the difference between C's arrays and pointers.)
>
> I don't understand this analogy. Can you explain please?

In numerous contexts,

   T a[N]

and

   T *a

are interchangeable. In fact, C has no rvalue notation for an array. For
any other type, this works:

   T a, b;
   a = b;

However, if T is an array type, the compiler complains:

   error: assignment to expression with array type

This C innovation of blurring the lines between arrays and pointers is
very different of the type system of Pascal, whose arrays behave like
any other type.

C could have treated arrays like other types without any loss of
generality. Then you'd have to write:

   T a[N], *b;
   b = &a[0];

for:

   T a[N], *b;
   b = a;

Semantically, as well, C arrays are iterables, and pointers are used to
iterate over the elements of an array.

Similarly, Python could have kept iterables and iterators in their
separate corners by specifying:

 * iter(iterable) returns an iterator

 * iter(iterator) typically raises an Exception

 * the for statement requires an iterator

Then, you'd have to write:

   for i in iter([1, 2, 3]):
       ...


Not recommending anything one way or another.


Marko



More information about the Python-list mailing list