I need to cultivate some new coding habits for Python 3 ready code
We have a Django application, and because of various aspects of Django (such as Django handling much of the heavy lifting), I expect that it's our most Python 3 ready chunk of code. Since I was working on it today anyways, I took a brief run at seeing if it would at least print help messages if I ran it under Python 3. As it turns out, making the attempt shows me that I need to cultivate some new coding habits in order to routinely write code that will be ready for Python 3.
What I stumbled over today is that I still like to write except
clauses in the old way:
try: .... except SomeErr, err: ....
The new way to write except
clauses is the less ambiguous 'except
SomeErr as err:
'. Python 3 only supports the new style.
Despite writing at least some of the code in our Django application
relatively recently, I still wrote it using the old style for
except
. Of course this means I need to change it all. I'm pretty
certain that writing except
clauses this way is not something
that I think about; it's just a habit of how I write Python, developed
from years of writing Python before 'except CLS as ERR
' existed
or at least was usable by me.
What I take away from this is that I'm going to need to make new
Python coding habits, or more exactly go through the more difficult
exercise of overwriting old habits with new ones. I'm sure to be
irrationally annoyed at some of the necessary changes, especially
turning 'print
' statements into function calls.
(If I was serious about this, what I should do is force myself to write only in Python 3 for a while. Unfortunately that's not very likely.)
The good news is that I checked some code I wrote recently, and I
seem to have deliberately used the new style except
clauses in
it. Now if I can remember to keep doing that, I might be in okay
shape.
(Having thought about it, what would be handy is a Python linter that complains about such unnecessary Python 3 incompatibilities. Then I'd at least have a chance of catching my issues here right away.)
PS: Modernizing an old code base is a related issue, of course. I need to modernize both code and habits to be ready for Python 3 in both current and future code.
Sidebar: The timing and rationality of using old-style except
New style except
was introduced in Python 2.6, which dates back
to 2008. However, the new version of Python didn't propagate into
things like Linux distributions immediately; it took two years to
get it into an Ubuntu LTS release, for example (in 10.04). Looking
back at various records, it seems that the initial version of our
Django application was deployed on an
Ubuntu 8.04 machine that would have had only Python 2.5. In fact I may have written the first version
of all of the substantial code in the application while we were
still using 8.04 as the host machine and so new-style except
would
have been unavailable to us.
This is of course no longer the case. Although not everything we
run today has Python 2.7 available (cf),
it all has at least Python 2.6. So I should be writing all my code
with new style except
clauses and probably some other modernizations.
|
|