Wandering Thoughts archives


Writing in Python 3 has been a positive experience so far

As I've mentioned in passing before, I have a little Python thing to log MIME attachment type information from Exim and I converted it from Python 2 to Python 3 about a month and a half ago. The direct reason for doing this was a relatively small one: the Python 3 version of the zipfile module could automatically handle ZIP archives (and tar files) that used XZ compression, which people were sending us. My indirect reason was that I haven't really done anything with Python 3 yet and it's clear to me that it's the future, so if I had an excuse it felt like it was time for me to get some experience.

The whole conversion went surprisingly smoothly. In large part this was because I'd written relatively clean modern Python code and I wasn't interpreting raw bytes off disk (or out of the network). Since I was mostly calling modules I could rely on them to do all of the hard work of thinking about Unicode; the actual change wound up being very small as a result. Of course that first pass involved some decisions that were kind of a hack, so I wound up having to do some real Unicode handling. I actually feel that this was a positive change for the code in general, since it forced me to think carefully about what I really wanted to do here instead of brushing it under the carpet of 'let's just spray random raw bytes at syslog and standard output'.

(Part of it is that doing the Python 3 equivalent of what I usually do here in Python 2 was just that little extra bit too much of a hack for me to accept.)

Python 3 character encoding issues were not painless and did force me to do some digging. In an ideal world this would be better documented, but on the other hand I don't entirely mind having to do some code reading (but that's just me, other people could wind up more irritated). I feel, perhaps incorrectly, that wrestling with these issues here has made me better prepared to deal with similar ones in the future.

So overall I'd call this a complete success. Moving to Python 3 got me real benefits, caused me to clean up the code somewhat, and wasn't particularly painful. I now feel much more positive about doing more substantial work in Python 3 at some point, and just generally working with/in Python 3 in the future.

(I'm still unlikely to convert any of our existing Python code over to Python 3 unless I get some clear benefit from it, the way I did here. I'm not yet that attracted to Python 3, and besides my co-workers would rather that I left well enough alone.)

python/Python3PositiveExperience written at 00:48:27; Add Comment

Page tools: See As Normal.
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.