Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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


Python 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.


D'oh. I put in a map(), that returns a list.


Turning 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.


Most of the breaking changes (including making map, zip, filter, etc lazy) were done in Python 3.0.


Ah, 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: