4 ms·
The example you gave works perfectly in Python 2.7 (would also be a generator, and you're not using map filter or else); but I agree: those should've been gener
by halflings 12y ago
The example you gave works perfectly in Python 2.7 (would also be a generator, and you're not using map filter or else); but I agree: those should've been generators from day 1, especially zip and enumerate since they make more elegant code but often come with a performance overhead in Python 2.7
- exDM69 12y agoAh, you're absolutely right. for line in open(filename) returns a generator. I added map(str.upper, xxx) there to make it more it make sense. But the point should be obvious, without generators that would potentially consume a lot of memory.
- danohuiginn 12y agoTurning them into generators would have broken a lot of existing code, though, so it's reasonable to leave them until a major version change. Rather, these are the kind of small, obviously-useful changes that should have come immediately in 3.0, giving people some encouragement to switch.
- exDM69 12y agoMost of the breaking changes (including making map, zip, filter, etc lazy) were done in Python 3.0.
- maxerickson 12y agoPython 2 enumerate returns an 'enumerate' object that is more or less a light weight wrapper of the sequence that was passed in. Generators provide a convenient syntax to implement that sort of object.
- exDM69 12y agoD'oh. I put in a map(), that returns a list.