Sameness: is vs ==
20-minute lesson · module-classes-deeper-03
You are previewing this course as a guest
Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.
Sameness: is vs ==
20-minute lesson · module-classes-deeper-03
- Distinguish identity (`is`) from equality (`==`).
- Explain why two distinct objects with equal contents are `==` but not `is`.
- Define `__eq__` so `==` compares contents for your own class.
- Predict the result of `is` and `==` for aliases, copies, and fresh objects.
Source attribution
Status: adapted
How to Think Like a Computer Scientist: Interactive Edition — source · Classes and Objects - Digging a Little Deeper / Sameness
Adaptation notes. Reading rewritten in our voice. A Point class shows that two separately constructed equal points are not identical until __eq__ is defined. Hidden tests check identity vs equality and a custom __eq__.
License notes. Original chapter is GNU FDL 1.3 (Brad Miller and David Ranum). No verbatim text from the original is reproduced here.