Python Dataclasses

Data classes are awesome, I first encountered them in Kotlin, but gradually the major languages have begun implementing them – in C# 9 and Java 15 they are known as records, although thanks to the excellent lombak you’ve been able to achieve a similar effect in Java for some time now. Data classes are a […]

Selection Sort in Python

The selection sort is a nice algorithm, it’s not the fastest but definitely a step up from the bubble sort. It is simple and easy to remember. With the selection sort: identify the smallest item in the source list extract it to the new ordered list repeat for each item in the source list Here […]

Binary Search in Python

The binary search is an excellent algorithm to find an item in a sorted list, being O(Log n) it is very efficient. This is an implementation in Python: Essentially it works like this: start with low set to the first item, high to the last, index to the middle if the item at the index […]

Assert in Python

Unit Testing and Static Analysis tools are an excellent means to help ensure the integrity of our software, but we don’t want to rely on them. They should be more like a second layer of verification. Ideally, we want our code to be self-validating. Our code should assert its own correctness. To help with this, […]

Exceptions in Python

Like C# and Java, Python uses special objects called exceptions to manage runtime errors. A runtime error is something that is beyond the developer’s control, such as a missing file. When such a condition occurs, Python raises an appropriate exception with related information for diagnosis. To raise an exception involves unwinding the application call stack […]

Conditional Expressions

Many of the features embraced by modern languages facilitate more concise expression; developers can do more with less. Importantly, they are introducing these features in an elegant, easy to read syntax. A delightful example of this is Kotlin’s .also to swap two variables. In the past we would might do something like this: The performance […]

*args and **kwargs

Python has two very useful idioms for passing a variable number of arguments to functions, *args and **kwargs. These arguments are used extensively when developing general-purpose libraries, decorators or proxies. But what are these these two mysterious words of pirate sounding origin? *args is a tuple of positional arguments (i.e. “fred”, “tom”, “betty”) **kwargs is […]