Where is the usage of (list comprehension) documented?

Thomas Jollans tjol at tjol.eu
Mon Jan 15 07:16:04 EST 2018


On 2018-01-15 00:01, Peng Yu wrote:
> Hi,
> 
> I see the following usage of list comprehension can generate a
> generator. Does anybody know where this is documented? Thanks.
> 
> $ cat main.py
> #!/usr/bin/env python
> 
> import sys
> lines = (line.rstrip('\n') for line in sys.stdin)
> print lines
> 
> lines = [line.rstrip('\n') for line in sys.stdin]
> print lines
> $ seq 10 | ./main.py
> <generator object <genexpr> at 0x1101ecd70>
> ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
> 

Generator expressions are of course documented in the language reference:

https://docs.python.org/3/reference/expressions.html#generator-expressions

The official docs.python.org tutorial also explains them:

https://docs.python.org/3/tutorial/classes.html#generator-expressions



More information about the Python-list mailing list