python and typing | Mar 28, 2025
Here’s a function with an idiom I’ve seen a lot (probably copied fromsentence-transformers
):
python and typing | Mar 20, 2025
Working with external untyped code in a typed code base can be challenging, you’ll get lots ofAny
orUnknown
, which might propagate through your codebase. This can force you to reach fortyping.cast
, or# type: ignore
statements, which kind of defeats the purpose of using static typing in the first place.
python and ml | Mar 14, 2025
Here’s a pop quiz: classifierA
scores 90% accuracy on some benchmark. ClassifierB
scores 80%. How much better isA
?
python and typing | Mar 7, 2025
Regular users of my blog will know that I am opposed to what is known asstringly typing: using strings in place of more strongly typed identifiers. As an example, consider a language-specific tokenizer:
python and unicode | Sep 3, 2024
Below are two ways to check if a string is lower-cased in Python.
python and typing | May 31, 2024
Caching, ormemoization, is a useful way to speed up repeated calls to expensive,pure, functions. When calling a function, we save the output, using the parameters of the function as a key to the cache. Then, instead of re-calculating the result of a function on each call, we simply return the value that was stored in the cache.
python and typing | Apr 8, 2024
New week, new post! This post is aboutNewType
, an underused construct in Python, in my opinion, and a good way to show the difference betweentypingtime andruntime.
python, typing, and unions | Mar 13, 2024
This post will be aboutUnion
s,TypeVar
s, and unions of types with a relevant common subtype (i.e., notobject
). I’ll show how union types are often incorrectly used, and how using aTypeVar
can solve some of these problems. So, having said that, let’s dive in!
python | Mar 4, 2024
In the previous post, I wrote about enumerations, and how they can be really handy when refactoring code. One thing I didn’t touch upon in that post is typed enumerations, which are enumerations that also have a type. As we saw in the previous post, an enumeration member is an association between aname
and avalue
. But this means we need to call.value
to get the actual value of an enumeration member. This can lead to overly verbose code. Take a logger for example:
python | Feb 22, 2024
Enumerations are types that take a set of pre-defined options, calledmembers which are also assigned values. Usually, enumerations members are, as the name implies, simply mapped to a integer values, but any arbitrary value might work. Here’s an example of an enumeration in Python for colors: