Member-only story
3 Essential Questions About Hashable in Python
From general discussion to specific implementation
As a general-purpose programming language, Python provides a good range of built-in data types for various use cases.
When you learned these basics, you have probably encountered the mentioning of hashable at certain points. For example, you may see that the keys in a dict
need to be hashable (see a trivial example in the code snippet below).
For another instance, it’s mentioned that the elements in a set
need to be hashable.
You may wonder — What does hashable mean exactly? Which objects are hashable and which are not? What will happen if we use unhashable objects as keys for dictionaries? And so on. Many related questions can be asked.
In this article, we’ll go over some key points about hashability such that you’ll learn how to address these questions. In the end, you’ll probably find out that these questions are actually not hard at all, unlike what you may have thought initially.