Deep comparison of dicts - cmp() versus ==?
Rustom Mody
rustompmody at gmail.com
Thu Mar 19 23:27:44 EDT 2015
On Friday, March 20, 2015 at 8:27:26 AM UTC+5:30, Victor Hooi wrote:
> Hi Ben,
>
> When I said "deep", I meant, as in, to an arbitrary level of nesting (i.e. dicts, containing dicts, containing dicts etc) - sorry if I got the terminology wrong.
>
> The two dicts weren't equal by intention - the idea was that a comparison operator would return "False" for those two.
>
> I was just curious why cmp() was phased out (as in, were there cases where "==" was better) - but if functionality they're the same, and it's just a nomenclature thing, that's also fine =).
>
> Finally, so cmp()/== return true/false for comparison...
I guess you are confusing two orthogonal questions:
1. deep vs shallow comparison
2. binary vs trivalent comparison
Numbers (not complex) satisfy the trichotomy law: ie for any 2 numbers x,y:
x < y or x > y o x = y
Unfortunately most logic in programming languages is binary -- true or false
(well Fortran had an arithmetic if -- very unfashionable even in Fortran --
but thats because of the need to goto)
Likewise C's strcmp is trivalent. But a trivalent value is clumsy in a binary logic. Unfortunately no switch statement for <, =, > -- so we are stuck with
if-else-if-else.
All this is unrelated to deep-vs=shallow: cmp is deep, == is deep
In fact what would/could a shallow comparison mean??
Shallow/deep only make sense for copy not comparison
[If at all: Languages like Haskell are explicitly constructed so that the
programmers is disallowed from making that distinction at all]
More information about the Python-list
mailing list