Comfort with Small Mistakes
It has been a long time since I learned how to program, and it is easy to forget some of the hard-won lessons in the beginning. Easy until I try to teach people to program. There is a lot of accidental and inherent complexity in programming, but I am ready …
read moreOr else:
This was originally sent to my newsletter. I send one e-mail, always about Python, every other Sunday. If this blog post interests you, consider subscribing.
The underappreciated else keyword in Python has three distinct uses.
if/else
On an if statement, else will contain code that runs if the condition …
Forks and Threats
What is a threat? From a game-theoretical perspective, a threat is an attempt to get a better result by saying: "if you do not give me this result, I will do something that is bad for both of us". Note that it has to be bad for both sides: if …
read moreMeditations on the Zen of Python
Precise Unit Tests with PyHamcrest
(This is based on my article on opensource.com)
Unit test suites help maintain high-quality products by signaling problems early in the development process. An effective unit test catches bugs before the code has left the developer machine, or at least in a continuous integration environment on a dedicated branch …
read moreRaise Better Exceptions in Python
There is a lot of Python code in the wild which does something like:
raise ValueError("Could not fraz the buzz:" f"{foo} is less than {quux}")
This is, in general, a bad idea. It does not matter if the exception is fairly generic, like ValueError or specific like CustomFormatParsingException …
read moreAn introduction to zope.interface
This has previously been published on opensource.com.
The Zen of Python is loose enough and contradicts itself enough that you can prove anything from it. Let's meditate upon one of its most famous principles: "Explicit is better than implicit."
One thing that traditionally has been implicit in Python is …
read moreAdding Methods Retroactively
The following post was originally published on OpenSource.com as part of a series on seven libraries that help solve common problems.
Imagine you have a "shapes" library.
We have a Circle
class,
a Square
class,
etc.
A Circle
has a radius
,
a Square
has a side
,
and maybe Rectangle …
Designing Interfaces
One of the items of feedback I got from the article about interface immutability is that it did not give any concrete feedback for how to design interfaces. Given that they are forever, it would be good to have some sort of guidance.
The first item is that you want …
read moreInterfaces are forever
(The following talks about zope.interface interfaces, but applies equally well to Java interfaces, Go interfaces, and probably other similar constructs.)
When we write a function, we can sometimes change it in backwards-compatible ways. For example, we can loosen the type of a variable. We can restrict the type of …
read more