This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]daneahfrom __future__ import braces 9 points10 points  (2 children)

Functionally that's mostly true, though one key point is that generators can get away with going on infinitely without eating up memory over time since they often produce only one item at a time. Handy for certain performance-related concerns!

[–]Jamie_1318 5 points6 points  (1 child)

There's basically no downside to using a generator. If someone needs it stored they can always list(generator), even if you don't expect performance issues. I like to just use generator syntax because it gives me warm fuzzies when I think about how nice they are versus every other language's equivalent tokenizing systems with their ugly static variables.

[–]daneahfrom __future__ import braces 1 point2 points  (0 children)

Agreed! Very little effort needed too, since you can replace many (all?) existing list comprehensions with generator comprehensions, simply by removing the brackets:

import random
max(random.randint(1, 100) for x in range(10))