How is the return value of __hash__ used?

前端 未结 3 1005
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 07:41

Suppose I write a class, but don\'t define a __hash__ for it. Then __hash__(self) defaults to id(self) (self\'s memory addres

3条回答
  •  礼貌的吻别
    2021-01-21 08:16

    When an object is stored in a dictionary, the __hash__ is used to determine the original bin that the object is placed in. However, that doesn't mean one object will get confused with another in the dictionary- they still check for object equality. It just means that the dictionary will be a bit slower in hashing that type of object than others.

提交回复
热议问题