On Lists and Iterables
Neal McBurnett
neal at bcn.boulder.co.us
Sat Dec 16 17:16:26 UTC 2017
As J Fernyhough notes elsewhere in the thread, this is not the list to quibble about Python 3.
Though, as John Lenton notes, there are excellent reasons for most of the changes in Python 3, which of course has a different leading version number precisely because it is not backward compatible. See e.g. this piece on the critical need for Python 3, and the benefits of it. E.g.
Why Python 3 exists https://snarky.ca/why-python-3-exists/
For more on the rationale for changes related to iterators see http://portingguide.readthedocs.io/en/latest/iterators.html
But it is very much appropriate for this list to help developers port packages so they can continue to be supported in future Ubuntu versions after Python 2 is demoted.
And you should be pleased to learn that often all you have to do is use the 2to3 or futurize tools to automatically port your code to Python 3. After doing so you can take advantage of the cleaner and more efficient Python 3 world. It is of course a hassle to do so, but it is a one-time cost, with many benefits.
For example here I port your one-line Python 2 script that uses zip.
$ cat porting_example.py
print zip(["a","b"], ["c","d"])
$ python porting_example.py
[('a', 'c'), ('b', 'd')]
$ 2to3 porting_example.py > porting_example.patch
$ cat porting_example.patch
--- porting_example.py (original)
+++ porting_example.py (refactored)
@@ -1 +1 @@
-print zip(["a","b"], ["c","d"])
+print(list(zip(["a","b"], ["c","d"])))
$ patch -b < porting_example.patch # -b saves original as porting_example.py.orig
$ cat porting_example.py
print(list(zip(["a","b"], ["c","d"])))
$ python3 porting_example.py
[('a', 'c'), ('b', 'd')]
See docs at http://python3porting.com/2to3.html
Porting Python 2 Code to Python 3 — Python 3.6.1rc1 documentation
https://docs.python.org/3/howto/pyporting.html
Neal McBurnett http://neal.mcburnett.org/
On Fri, Dec 15, 2017 at 11:40:14AM +0100, Xen wrote:
> I am just posting this so I don't have to save the text.
>
>
> 2.7: type(zip(["a","b"], ["c","d"]))
> <type 'list'>
>
> 3 : type(zip(["a","b"], ["c","d"]))
> <class 'zip'>
>
> 3 : zip(["a","b"], ["c","d"])
> <zip object at 0x7f684fd71708>
>
> 2.7: zip(["a","b"], ["c","d"])
> [('a', 'c'), ('b', 'd')]
>
> 3 : list(zip(["a","b"], ["c","d"]))
> [('a', 'c'), ('b', 'd')]
>
> I don't even know what Iterables are.
>
> I just know that I can't print them directly.
>
> Python is supposed to be a beginner-friendly language.
>
> But this is incomprehensible.
>
> Zipping by definition produces a list of tuples, I mean zipping a
> list by definition creates a list of tuples, not a "zip" object.
>
> The whole semantic definition of "zip" is to take 2 (or more) lists,
> then create tuples out of every matched list element, and return
> those as a new list.
>
> Not to be left in some intermediate stage which is somehow more
> efficient to the interpreter or something.
>
> That would be like calling Set.difference and then getting a
> Difference object instead of a Set.
>
> Just an example of a user-unfriendly change.
>
> I already know someone is going to say "No it's not." and then leave
> it at that.
>
> About the above.
>
> Regards.
More information about the Ubuntu-devel-discuss
mailing list