Member-only story
6 Alternatives to Classes in Python
Speed of development, execution time, (de)serialization, and maintainability all play a role in making your code shine

As developers, we throw a lot of data around. The representation of data matters a lot and we need to be able to keep track of which variables represent which attributes. Configuration is a prime example of complex data.
In the following article, I will use location as an example. It must have a longitude, latitude, and can have an address. In C, you would use a struct
for this. In Java, you would simply create a class. In Python, there are six alternatives. Let’s explore each of their advantages and disadvantages!
Plain Classes
Plain classes are the default way provided by the standard library to organize data. You can (and should!) use type annotations as done in the following example:
You can see how we needed to write a boilerplate constructor __init__
. The code for the constructor does not necessarily always look that simple, but in a lot of cases, it is.
You can see that it’s possible to use positional arguments or keyword arguments. If you define a default value in the constructor, you can also leave the values out when you create an object from the class. This happened for pos2
, where the address
was not given to the constructor.
You can also see that the annotation for the get_distance
function looks pretty clean. It is clear what is meant.
The tooling support is as good as it gets because every editor has to support the plain classes and all of the important information is there.
1. Tuples
Tuples are a native data type. They have a very low memory overhead, so we can address the elements by index very quickly. The problem with tuples is that you have no names for member attributes. You have to remember what each index represents. Tuples are always…