Python 3

This website now runs on python 3.

2019-04-30 21:42

Python 2 will reach its Sunset date by the end of the year (2019) and I kind of wanted to wean myself of 2.7 so I set about converting this project. The conversion of pymcms and its applets works handily as a small trial before converting any of the big work projects.

print()

There are a couple of things that aren't just new things but "breaking" changes between 2 and 3. The big noticeable one is that print now is a function and you can't just do print "foo" anymore but have to type print("foo"). This is, however, not a thing that trips you up.

Subtleties

Python will as of version 3 no longer return a list (implicitly) when calling dict.keys() or dict.items() but instead give you an object that references the items directly which means that you have to be careful not to change the contents of the dictonary when looping over it, or converting it with list() which will create a copy of the values.

There are also concurrency issues with this, but I generally don't write particularily heavily multithreaded python code so it's not an issue for me.

Since the results of keys() no longer is a list, you can't depend on it being sort()able which gotcha'd me a bit.

Python has for a while now come with a tool, 2to3 that helps with the transition but it is a bit odd, that it will defensively wrap every call to keys() and items() with list() whether they need it or not. It also completely missed my sorting of a list using the cmp parameter which no longer exists in python 3. So beware, but it's a useful too, especially for package local imports which are bound to be everywhere. I'm guessing it's trying not to be to smart and doesn't actually parse the language but just finds common issues.

They say any story should have a distinct start, a middle and an end. This one doesn't.

Staffan