by exDM69 · 11y ago · 0 comments
To me, the killer feature is better lazy evaluation (generators). In particular, important builtins like map, filter, zip, enumerate, etc are generators, instead of returning lists. This makes it feasible to write things like
(process(line) for line in map(str.upper, open('giantfile.txt')) if line.lstrip()[0] != '#')
Some of the above can also be done with itertools package in Python 2, but not everything.Python 3.4 changelog is here, it contains e.g. asynchronous io facilities (asyncio module): https://www.python.org/downloads/release/python-342/
edit: added enumerate() in the example above, for line in open(filename) returns a generator in Python 2.x too.
edit2: enumerate is lazy in python2, I replaced it with map(str.upper)
Generators provide a convenient syntax to implement that sort of object.
But the point should be obvious, without generators that would potentially consume a lot of memory.
(process(line) for line in open('giantfile.txt') if line.lstrip()[0] != '#')
Is that line really using any new features in Python 3? The lazy evaluation there is in the file object and the generator expression, both of which have long been present in python2. (process(line.upper()) for line in open('giantfile.txt') if line.lstrip()[0] != '#')Here's another one you can't change that easily:
tests_pass = all(process(input) == output for (input, output) in zip(open('inputs.txt'), open('outputs.txt'))Changing that to line.lstrip().startswith('#') would be an alternate approach.