Main Content

Teaching dilemma - old tricks and techniques, or recent enhancements?

Archive - Originally posted on "The Horse's Mouth" - 2011-10-08 14:58:19 - Graham Ellis

Where there's something that's a frequent requirement on one of the subjects we teach, but can be hard to achieve, we'll spend more than just a minute or two covering it on our courses. After all, the tips and techniques of how to make the most of a programming language are every bit as important as the basic structures of the language itself. I was taught something similar to this during a brief (but memorable) sales training course - "don't tell people about the features of your product, but rather tell them about the benefits".

Formatting currency values in Python - with commas every 3 digits - wasn't straightforward a couple of years ago. It needed not only the % formatting operator, but also another mechanism to add the commas in - and there's an example that we used on our Python course, using a class of objects of a type we called "currency" and regular expressions. It's [here] if you want to see the source, it works well, and it's what I would use up to and including Python 2.6

From Python 2.7, though, an extra comma may optionally be provided in the format string so that {:,.2f} may be uses as the descriptor for a currency value with 2 figures after the decimal point, and with a comma every three digits. The expression looks a bit obtuse, but it's a natural extension on the end of format training and can be covered in a few seconds. A new sample on our web site - [here] - is less than half as long as the previous one, and it works neatly.

So - do we celebrate the new ease? Do we yearn for the good old days? Which do we teach?

Yes, we do celebrate. This is bringing the language forward and making it better for the future. While there's still a significant proportion of older (pre-change) systems out there, we'll maintain comment and coverage in the course, and indeed the example will remain in the notes for rather longer as many delegates will have to maintain systems written using the older style even on newer systems.

New systems / methods are talked about as soon as they're in a significant proportion of user's systems. There has been only limited reason to teach people about Python 3 so far, but we're now into a much stronger changeover period. And with such big changes we have already been waving a flag on the horizon for several years. With the string formatter, my main example in this article, it's going to be mentioned on every course from this point forward even though it was only new in 2.6, and enhanced in 2.7. Older examples on the web site will remain, but as they're used and reused I'll be adding comments to say "now use format" and perhaps to give the alternative.

We celebrate and encourage languages to move forward. But we respect and support our customers with established code written using earlier releases and their need to maintan the code written with those releases