5 ms·
(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
by danohuiginn 12y ago
(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.
- exDM69 12y agoNo, it wasn't. I added map(str.upper, xxx), now it does.
- masklinn 12y agoThen again a lazy map is just an import away in P2. It removes pitfalls from Python but the improvement is… very limited (as opposed to e.g. `yield from` which is a big convenience, or for much more specialised uses the ellipsis literal being universal)
- dalke 12y ago(process(line.upper()) for line in open('giantfile.txt') if line.lstrip()[0] != '#')
- exDM69 12y agoThat's besides the point. It's trivial to write that as a for loop or in a million different ways to avoid the issue. It's a contrived example written to demonstrate a difference. 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'))
- andreasvc 12y agoYou can change that easily, with izip from itertools. The fact that a bunch of builtins and the values/items methods of dictionaries have become iterators is not very siginificant IMHO. Python 2 code could already be written to use iterators or generator expressions, so in the parts where it was crucial it was already done. In this regard Python 3 has not added new functionality but only changed defaults. The unicode change is the big one.
- dalke 12y agoIn this case (checking that process(input) == output), itertools.izip_longest is probably that right solution, unless there's an out-of-band way to know that inputs.txt is the same length as outputs.txt.
- dalke 12y agoYou felt the need to correct yourself earlier, so I think your "besides the point" should be directed to yourself. I was pointing out that your correction wasn't persuasive.