Skip to main content
In Part 3 we concluded the “basic” knowledge that you need to understand classes. This article and subsequent ones will cover topics on more of an ‘intermediate’ level. Starting with the (im)mutability of dataclasses.

dataclasses aren’t hashable

In Python it’s important to know if things are ‘hashable’ or not. A “hash” is a fixed-size (usually small) value that is used as a unique label for something else. However, for the label to be unique the thing it is pointing to mustn’t change, otherwise that means two things could be the same but have different hashes and that would break a lot of stuff. Only fixed objects that can not be changed can be hashed, in other words only “immutable” objects can be hashed. Data classes are “mutable” by default and thus can be changed and can not be hashed. However hashing is very useful and keeping things immutable has other benefits as well. Hashes are used to make memory searches of collections much faster as well as enforce uniqueness in a collection. There are two very common objects in Python that rely on hashing: sets and dictionaries. sets in Python are collections of hashes and you can never have non-unique values in a set, which has many uses. dicts extend this to what’s known as a “Hash map”. dicts contain a collection of hashes that map onto whatever object you choose to point them at. All dictionary keys must be hashable, but the values don’t have to be. For example, take the Coordinate and ThreeDimensionalSpace classes from the previous parts.
It would make sense that coordinates is a set and not a list because there should never be multiple coordinates referring to the same point in 3D space. That’s an impossibility, so we reflect that in the class definition. However, as is, this won’t work! Data classes are mutable by default. So if we want it to we need to fix Coordinate and make it immutable. We can do this very simply, by including an additional parameter in the dataclass decorator on Coordinate: frozen=True.
It works! Sets and dictionaries are very useful for maintaining uniqueness and great for searching for things. For example, we could now make a new method “is_2d_position_in_space” on ThreeDimensionalSpace that takes two values, an x and a y value, and tells you if this coordinate is present at any z of the 3D space. Such a method might look like this.
This is pretty useful. Note that there is no restriction on space being cubic. This code would work just as well for spherical, cylindrical, or even toroidal space. All you would need to do is change the properties stored.

Part 5: classmethod and staticmethod

With mutability out of the way you should be getting better and better at using data classes. Next up is all about the non-standard methods that do not use self like most classes do. In particular, the next part will cover classmethod and staticmethod, two tools for better classes. For a complete look at the articles in this series, have a look at this overview page: Getting started with classes in Python