a = dict(one=1, two=2, three=3) b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} c = dict(zip([‘one‘, ‘two‘, ‘three‘], [1, 2, 3])) d = dict([(‘two‘, 2), (‘one‘, 1), (‘three‘, 3)]) e = dict({‘three‘: 3, ‘one‘: 1, ‘two‘: 2}) print(a == b == c == d == e) # True from collections import abc print(isinstance(a, abc.Mapping)) # True # 1. The main value of the ABCs is documenting and formalizing the # minimal interfaces for mappings, and serving as criteria for isinstance # tests in code that needs to support mappings in a broad sense. print(hash((1, 2, (3, 4)))) # -2725224101759650258 # 1. An object is hashable if it has a hash value which never changes # during its lifetime (it needs a __hash__() method), and can be compared # to other objects (it needs an __eq__() method). # 2. Hashable objects which compare equal must have the same hash value. # 3. (atomic immutable types / frozen set / tuple when all its items are hashable) # 4. User-defined types are hashable by default because their hash value is their # id() and they all compare not equal. If an object implements a custom __eq__ # that takes into account its internal state, it may be hashable only if all its # attributes are immutable.
原文:https://www.cnblogs.com/lb477/p/10925072.html