🔥 BLACK FRIDAY SALE1 course ₹199·any 3 courses ₹399 for 3 months·ALL courses + 1 mock interview ₹599 for 6 monthsClaim offer →

Python · The Python Data Model · Intermediate · 26 min read

Hashing, Equality and Immutability in Python: Safe Value Objects, Dict Keys and Frozen Data

Design objects that work correctly in sets and as dictionary keys. Learn identity versus equality, the hash/eq contract, how Python makes classes unhashable, building immutable value objects (frozen dataclasses, slots, __setattr__ guards, NamedTuple), caching hashes, MappingProxyType and frozenset, hashing pitfalls (mutable keys, NaN, float and int equality), and when immutability pays off.

Hashing, Equality and Immutability in Python: Safe Value Objects, Dict Keys and Frozen Data: lesson illustration

A first look

  • Intermediate level · 26 min read
  • 7 interview questions with model answers
  • The Python Data Model · Python Mastery

You will learn to

  • Identity (is) versus equality (==) versus hash
  • The contract between eq and hash, and what Python does if you break it
  • Building immutable value objects in several ways
  • …and 3 more topics inside
Create an account to start

Create a free account to see what this lesson covers

  • Every free lesson in every section, in full
  • Track your progress and pick up where you left off
  • Free practice questions and an incident simulation
  • Takes under a minute: name, email and a password

No card needed. Full access to Python is a paid upgrade you can choose later.