Reading through this really cool online book, Speaking JS, I came across a neat quirk illustrating how comparisons work in JavaScript:
Primitive values are "Compared by value":
> 3 === 3
true
> 'abc' === 'abc'
true
Objects, however, are "Compared by reference":
> {} === {} // two different empty objects
false
> var obj1 = {};
> var obj2 = obj1;
> obj1 === obj2
true
A co-worker and I were chatting about this and wondered if the principle holds for Python.
So we cracked open a Python interpreter to see if this comparison works differently in that language.
>>> 3 == 3
True
>>> {} == {}
True
Turns out, two dictionaries resolve as equal in Python if their contents are the same.
Does this mean that Python dictionaries are "Compared by value"?
Is there a way to compare Python dictionaries by reference?