Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Since latest python version retains insertion order of dict,will the meaning of equality (==) change?

In latest python version, dict retains the order of insertion. Is there any change in terms of equality. For example, currently the below works. Since insertion order will be important, can this change in future?

I am asking because there is fundamental change - previously == worked because insertion order was not important as it was considered un-ordered. Now since it is ordered, can the meaning of equality change?

d1={'a':1,'b':2}
d2={'b':2,'a':1}
print(d1==d2)
True

l1=['a','b']
l2=['b','a']
print(l1==l2)
False
like image 759
variable Avatar asked Oct 04 '19 10:10

variable


1 Answers

Python's official documentation states the following about the == operator regarding dictionaries:

Mappings (instances of dict) compare equal if and only if they have equal (key, value) pairs. Equality comparison of the keys and values enforces reflexivity.

So, insertion order is not considered, and due to backwards compatibility, it probably never will be, as it probably wouldn't make sense, or be unintuitive, in almost all cases.

like image 96
Alan Verresen Avatar answered Nov 14 '22 22:11

Alan Verresen