Contrary to popular belief, Python was not named after a snake

Bye-bye, Python2

Fabian Neuschmidt
VIPERdev

--

Together with the end of the decade we are also, finally, experiencing the end of development of the Python 2 language. Python 2 will not go away, of course. You’ll likely find pictures of Putin using it 6 years from now. Coincidentally, I’m working on my first Python 2 based project for university right now.

When I first looked into Python in 2010, Python 3 was only two years old and had a much smaller ecosystem. NumPy and Django supported Python 2, only. Being relatively new to programming, still, I decided to learn Python 3 from the start and not bother with the differences in syntax etc.

It was definitely the right decision and the ecosystem grew faster than I did. coala was written in Python 3 from the start, as was everything written for GitMate (and VIPERdev, as far as I know).

In fact, I have never written any significant amount of Python 2 code until just recently: For a robotics class I currently have to use the ROS framework and Gazebo to teach a car to drive autonomously. ROS not only requires Python 2, but apparently also only supports Ubuntu 16.04 and 18.04. It is possible to compile it or install it on Arch Linux via the AUR, but it’s a lengthy process and I did not manage to run the course project on Arch Linux or OpenSUSE.

Anyway, Python 2 is a fine language and mostly works as you would expect. Still, there are a couple of things to know and pitfalls to avoid if you ever have to go back to Python 2:

Integer Division

This one got me good:

# python3
>>> 1/2
0.5
# python2
>>> 1/2
0

In Python 2 you have to manually cast one of the integers to a float. All of these work:

# python2
>>> float(1)/2
0.5
>>> 1./2
0.5
>>> 1/2.
0.5

If in doubt: from __future__ import division makes division work like Python 3.

JavaScript levels of comparison enthusiasm

In Python 2, you can pretty much compare anything to everything.

# python2
>>> "one" > 2
True
>>> None < 2
True
>>> None < len
True

This is not a problem in an of itself, but coming from Python 3, you will likely expect these things to crash and burn as they should — and they won’t.

Unicode

Python 2 has two text types: str and unicode. I will stay with ASCII characters if I’m writing Python 2.

# python 2
>>> s = 'ÖÄÜ'
>>> s
'\xc3\x96\xc3\x84\xc3\x9c'
>>> len(s)
6
>>> s = u'ÖÄÜ'
>>> s
u'\xd6\xc4\xdc'
>>> len(s)
3
# python3
>>> s = 'ÖÄÜ'
>>> s
'ÖÄÜ'
>>> len(s)
3

Lots of lists

A lot of things are lists in Python 2.

# python2
>>> x = {"a":1}
>>> type(x.keys())
<type 'list'>
>>> type(x.values())
<type 'list'>
>>> type(x.items())
<type 'list'>
>>> type(range(2))
<type 'list'>
# python3
>>> x = {"a":1}
>>> type(x.keys())
<class 'dict_keys'>
>>> type(x.values())
<class 'dict_values'>
>>> type(x.items())
<class 'dict_items'>
>>> type(range(2))
<class 'range'>

Modern stuff

Most everything else is similar to Python 3, but of course some new, shiny stuff is missing. Advanced unpacking, f-strings, yield from

That’s it for now. I hope you learned something and I hope you don’t have to apply it. Stay with Python 3, it’s awesome.

Happy New Year everyone, guten Rutsch!

--

--