How to identify a generator vs list comprehension
问题 I have this: >>> sum( i*i for i in xrange(5)) My question is, in this case am I passing a list comprehension or a generator object to sum ? How do I tell that? Is there a general rule around this? Also remember sum by itself needs a pair of parentheses to surround its arguments. I'd think that the parentheses above are for sum and not for creating a generator object. Wouldn't you agree? 回答1: You are passing in a generator expression. A list comprehension is specified with square brackets ( [.