4 ms·
Generators have a few footguns even for seasoned developers, calling len(mygen) or mygen[4] works, but after that it's empty when trying to iterate it. Particul
by Too 4y ago
Generators have a few footguns even for seasoned developers, calling len(mygen) or mygen[4] works, but after that it's empty when trying to iterate it. Particularly problematic is when someone adds a log of the generator content, exhausting it before before it is used. All this leads to people shotgunning mygen=list(mygen) on everything, without understanding what really happens.
How one could solve this i don't really know, generators are such useful concept that you can't really remove them either, and you still want them to be compatible everywhere other lists and iterables are. Even with type hints, it's not exactly obvious when you type the code whether a certain variable is a generator or a concrete list, both of them mostly adhere to the same protocol.