Difference between generator expression and generator function

Posted by Neil G on Stack Overflow See other posts from Stack Overflow or by Neil G
Published on 2014-06-04T09:20:17Z Indexed on 2014/06/04 9:24 UTC
Read the original article Hit count: 335

Filed under:
|

Is there any difference — performance or otherwise — between generator expressions and generator functions?

In [1]: def f():
   ...:     yield from range(4)
   ...:

In [2]: def g():
   ...:     return (i for i in range(4))
   ...:

In [3]: f()
Out[3]: <generator object f at 0x109902550>

In [4]: list(f())
Out[4]: [0, 1, 2, 3]

In [5]: list(g())
Out[5]: [0, 1, 2, 3]

In [6]: g()
Out[6]: <generator object <genexpr> at 0x1099056e0>

© Stack Overflow or respective owner

Related posts about python

Related posts about python-3.x