Confusion about iterator types

Kirby Urner urner at alumni.princeton.edu
Sat Aug 11 18:30:15 EDT 2001



In types, I find SequenceIterType and FunctionIterType.

When I go:

  >>> mylist = [1,2,3,4]
  >>> myiter = iter(mylist)

I then get:

  >>> type(myiter)
  <type 'iterator'>

But 'iterator' by itself has no corresponding type in types.  

If I use the isinstance() method, I can confirm that myiter is a
SequenceIterType, but shouldn't it just tell me that?  Shouldn't 
type(foo) return a type name that more closely matches its type 
in types?  Why not have?:
 
 >>> type(myiter)
 <type 'sequence-iter'>

Also, some clarification re the below would be helpful:

  >>> def f(n): return n*n

  >>> callable(f)
  1
  >>> k = iter(f,10)
  >>> type(k)
  <type 'callable-iterator'>
  >>> k()
  Traceback (most recent call last):
    File "<pyshell#142>", line 1, in ?
      k()
  TypeError: object is not callable: <callable-iterator object at 00B6D10C>

In IDLE, when I go >>> iter(  I'm prompted that the syntax is 
iter(collection) -> iterator.  Yet the PEP leads me to this other use 
of iter(v, w) where v must be callable.

Simply obeying that rule allows me to get as far as defining f above
(callable) and getting some callable-iterator k.  I don't know what this 
should mean or do in this context, but it's apparently legal -- until I 
actually try to call this callable-iterator, and get the error message that
this object isn't callable.  Just from a logical consistency point of view,
it seems that any object calling itself a callable-iterator shouldn't turn
around and tell me it ain't callable.

And again, there's no 'callable-iterator' type in types.

I find that my object is a FunctionIterType:

 >>> isinstance(k,types.FunctionIterType)
 1

Seems the types name should therefore be CallableIter or the returned
type should be <type 'function-iter'> (corresponding to <type 'sequence-iter'>).

Kirby




More information about the Python-list mailing list