[Tutor] implied tuple in a list comprehension
Steven D'Aprano
steve at pearwood.info
Fri Aug 2 10:27:38 CEST 2013
On 02/08/13 18:02, Jim Mooney wrote:
> I see what you mean, but I figured it can't be ambiguous if one
> interpretation makes no sense, and I can't see what x = [idx, (word
> for idx, word in S)] could possibly mean. Am I assuming too much
> foresight on the part of the parser or does that actually mean
> something?
It makes perfect sense. You can try it yourself, if you pre-define some names that get used:
idx = 42
S = [(0, "fe"), (1, "fi"), (2, "fo"), (3, "fum")]
x = [idx, (word for idx, word in S)]
print(x)
=> prints [42, <generator object <genexpr> at 0xb7b7098c>]
Hmmm, that's interesting. What's a generator object?
py> next(x[1])
'fe'
py> next(x[1])
'fi'
py> next(x[1])
'fo'
py> next(x[1])
'fum'
py> next(x[1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
--
Steven
More information about the Tutor
mailing list