[Tutor] List comprehensions

Kent Johnson kent37 at tds.net
Thu Jan 13 16:52:40 CET 2005


Blake Winton wrote:
> Kent Johnson wrote:
> 
>>>> If you mean for j to be a list of foobar(item) then use
>>>> j=[foobar(item) for item in x]
>>>> The first part of the list comp can be any valid expression.
>>>
>>> Does that mean that there are invalid expressions? I'd enjoy seeing 
>>> an example.
>>
>> I suppose if it's an expression, it must be valid, eh? Otherwise it's 
>> something else.
> 
> 
> I don't think I entirely agree...  What about "x === item"  It's 
> certainly not a statement, and I would wager that Python was in the 
> middle of its expression parsing code when it threw the SyntaxError. (Or 
> how about "x == item ="?)  Perhaps someone more in touch with the 
> compiler internals will chip in here.

We're talking about angels and pins, here, but I would say that the only meaningful interpretation 
of 'x is an expression' is 'x is parseable according to the syntax rules for an expression'. So if 
it doesn't parse it's not an expression.

How can you say "x === item" is not a statement? Because it doesn't parse as a statement. By the 
same logic it isn't an expression either.

Kent

> 
> It is an interesting philosophical question, though.
> 
> Later,
> Blake.
> 


More information about the Tutor mailing list