[Tutor] List comprehensions
Bob Gailer
bgailer at alum.rpi.edu
Thu Jan 13 05:13:09 CET 2005
At 07:05 PM 1/12/2005, Kent Johnson wrote:
>I suppose if it's an expression, it must be valid, eh? Otherwise it's
>something else.
At 06:41 PM 1/12/2005, Max Noel wrote:
>On Jan 13, 2005, at 01:13, Bob Gailer wrote:
>
>>At 04:48 PM 1/12/2005, 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.
>
>Here's an obvious one:
>
>j = [foobar(item)/0 for item in x]
I like Kent's response.
foobar(item)/0 is a "valid" expression. It fits the grammar of expressions.
The fact that it raises an exception does not make it an invalid expression.
Consider foobar(item)/xyz. It is valid. If xyz == 0 then it will also raise
an exception.
Bob Gailer
mailto:bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell
More information about the Tutor
mailing list