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.
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.
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.
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