Suggestion for impriving list comprehensions

Tom Good Tom_Good1 at excite.com
Mon Jul 30 12:20:37 EDT 2001


Neil Schemenauer <nas at python.ca> wrote in message news:<mailman.996282541.32450.python-list at python.org>...
> Tom Good wrote:
> > Consider the following:
> > 
> > >>> from __future__ import generators
> > >>> def g():
> > ...     yield 1
> > ...     yield 2
> > ...
> > >>> i = iter(g)
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > TypeError: iter() of non-sequence
> > >>>
> > 
> > iter(g) doesn't work, but it somehow feels as if it should. 
> 
> What could iter(g) possibly mean?  This seems to be some very weird
> confusion about a function object and the object a function returns.
> Are you surprised when:
> 
>     def one():
>       return 1
> 
>     print one + 2
> 
> does not print 3?
> 
>   Neil

No, I'm not surprised by the result in your example.  And when I stop
and think about it, of course iter(g) doesn't work.  *At a glance*
though, "iter(g)" (where g is a generator) and "iter(i)" (where i is
another iterator) look very similar, but the latter works and the
former is an error.


Tom



More information about the Python-list mailing list