by pdknsk · 11y ago · 0 comments
https://www.python.org/dev/peps/pep-0448/ https://www.python.org/dev/peps/pep-3132/
I'd be really nice to use this.
>>> [*range(i) for i in range(5)]
Instead of this monstrosity right now. >>> [x for y in (range(i) for i in range(5)) for x in y]
Python 2.7 has some minor features that 3 dropped unfortunately, which still makes me hesitate.Such as filter keeping the type. In 3 it returns an iterator.
>>> filter(lambda x: x in 'ABC', 'ABCDEFA')
'ABCA'
Or this mostly cosmetic feature. >>> filter(lambda x: x[0] > x[1], ((1, 2), (4, 3)))
>>> filter(lambda (x, y): x > y, ((1, 2), (4, 3))) # equivalent, error in 3
Also dropped. (It's slower than using the dedicated base64 module though.) >>>'Python'.encode('base64')
'UHl0aG9u\n'
Also, I like print. [*range(i) for i in range(5)]
and {**d for d in ds}
but it was removed ultimately because people in dev-python found it confusing. If you're interested in seeing that construction in Python, I suggest waiting until 3.5 gains some traction and then making a suggestion in python-ideas.FWIW, I'm the PEP writer. It's not my invention, I just wrote it up. It still makes me glad when people mention the PEP, though :).
> >>> filter(lambda x: x in 'ABC', 'ABCDEFA') > 'ABCA'
it was a special case for a few types, didn't (couldn't) work for all types e.g.
>>> filter(lambda x: x in 'ABC', set('ABC'))
['A', 'C', 'B']
> Also, I like print.I like Python 3's print. It's kind-of a pain because my day-to-day uses Python 2 and the context switch is annoying, but 3's is way more flexible and readable:
print <<f, foo, bar # (or is it >>file? I can never remember)
versus actually makes sense, no sigil soup
||
\/
print(foo, bar, file=f, flush=True)
/\
||
separate statement necessary in P2
and because it's an expression it can be used in more context than Python 2's print statement.I feel you on the loss of argument unpacking, it was a very convenient feature (though not too common IME)
>>> from __future__ import print_function
>>> print("hi")
hi
>>>
My team is starting to use it as we look to migrate various command line utilities to python 3. >>> sum((range(i) for i in range(5)), [])
[0, 0, 1, 0, 1, 2, 0, 1, 2, 3]
and >>> import itertools
>>> list(itertools.chain.from_iterable(range(i) for i in range(5)))
[0, 0, 1, 0, 1, 2, 0, 1, 2, 3]
"filter keeping the type". That was only true for some types. Python 2.7's filter does not maintain the set type: >>> filter(lambda x: x in 'ABC', {'A','B','C','D','E','F','A'})
['A', 'C', 'B']
Regarding the cosmetic feature, that's a consequence of PEP 3113 -- Removal of Tuple Parameter Unpacking , https://www.python.org/dev/peps/pep-3113/ , which also applies to functions.I use 's.encode("hex")' often. I realize why hex/base64 are gone, but it's so short and easy in Python 2.7.
This won't work in Python 3 since `range` is a new type (and it's O(n²) anyway, so not missed).
The second option is idiomatic IMHO.
>>> for i in range(20):
... t1 = time.time()
... n = len(sum((list(range(i)) for i in range(2**i)), []))
... t2 = time.time()
... print(i, t2-t1)
...
0 0.00010800361633300781
1 2.288818359375e-05
2 2.5033950805664062e-05
3 4.00543212890625e-05
4 7.295608520507812e-05
5 0.00017499923706054688
6 0.0005929470062255859
7 0.0024929046630859375
8 0.012469053268432617
9 0.14577507972717285
10 1.7278220653533936
11 17.82376503944397
^C
(MHO too, BTW.) >>> [x for y in (range(i) for i in range(5)) for x in y]
can be simplified into: [y for x in range(5) for y in range(x)]That really bugged me for a while, but then I added a "snippet" into my editor. Now I type: pr then the tab-key, and it transforms to:
print('', ) # with cursor between quotes
and it works well, also I have similar shortcuts for logging. >>> codecs.encode(b'Python', 'base64')
b'UHl0aG9u\n'
The encodings still exist; hex works too (or hex_codec in older Python 3s). Python 3 just restricted the encode/decode methods to always go str to bytes or bytes to str.