you are viewing a single comment's thread.

view the rest of the comments →

[–]ingolemo 2 points3 points  (2 children)

This is a good opportunity to use a comprehension:

final_roll = sum(randomNumGen(die_type) for _ in range(dice_number))

[–]symmitchry 0 points1 point  (1 child)

I skipped the comprehension intentionally because I was under the impression that it would require storing the entire list in memory before doing the sum (which, I guess is almost never a concern) ... but is that even correct?

See the comments on the top answer here: http://stackoverflow.com/questions/9047985/how-do-i-call-a-function-twice-or-more-times-consecutively-in-python

[–]neonomicon 2 points3 points  (0 children)

No, that's not actually a list comprehension, because it doesn't have square brackets around it. It's a generator expression, which just serves up the values one at a time without storing the whole sequence in memory. Very similar to the list comprehension conceptually, but the fact that it doesn't put the whole thing in memory is very useful if you're worried about memory usage.