PEP 276 -- What else could iter(5) mean?

Tim Peters tim.one at comcast.net
Mon Mar 4 03:03:19 EST 2002


[David Eppstein]
> Here's another way of looking at the same question.
> An iterable object has a next() function, that's what it means to be
> iterable.

An iterable object A is one that iter(A) doesn't barf on <wink>.  For
example, lists are iterable objects but don't have a .next() method.  It's
an iterat*or* that needs a next() method; an iter*able* object is one that
iter() knows how to get an iterator for.

>>> [].next
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'list' object has no attribute 'next'
>>> iter([]).next
<method-wrapper object at 0x0078D660>
>>>

> If numbers are iterable, we can call number.next(), right?

It's such a pretty point I hate to spoil it, but no.  If numbers are
iterable, then we can call iter(number).next().

> So what should 5.next() be?

SyntaxError as given, AttributeError if "(5).next()" instead, and by the
time we get to the "iter(5).next()" that should actually do something
besides blow up, the presence of iter() spoils the point.

> Surely anyone familiar with the Peano axioms would say 6, not 0!

If ints did have a .next() method, then I expect most people would expect 6
even if Peano had said succ(i) == -42 <wink>.





More information about the Python-list mailing list