This Hashable package defines a class, Hashable, for types that can be converted to a hash value. This class exists for the benefit of hashing-based data structures. The package provides instances for basic types and a way to combine hash values.
Questions tagged [hashable]
161 questions
138
votes
11 answers
Why can't I use a list as a dict key in python? Exactly what can and cannot be used, and why?
I found that the following are all valid:
>>> d = {}
>>> d[None] = 'foo'
>>> d[(1, 3)] = 'baz'
Even a module can be used as a dict key:
>>> import sys
>>> d[sys] = 'bar'
However, a list cannot, and neither can a tuple that contains a list:
>>>…

wim
- 338,267
- 99
- 616
- 750
68
votes
5 answers
What makes a user-defined class unhashable?
The docs say that a class is hashable as long as it defines __hash__ method and __eq__ method. However:
class X(list):
# read-only interface of `tuple` and `list` should be the same, so reuse tuple.__hash__
__hash__ = tuple.__hash__
x1 = X()
s…

max
- 49,282
- 56
- 208
- 355
46
votes
2 answers
Swift: 'Hashable.hashValue' is deprecated as a protocol requirement;
I've been facing following issue (it's just a warning) with my iOS project.
'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'ActiveType' to 'Hashable' by implementing 'hash(into:)' instead
Xcode 10.2
Swift 5
Source…

Krunal
- 77,632
- 48
- 245
- 261
44
votes
2 answers
Make struct Hashable?
I'm trying to create a dictionary of the sort [petInfo : UIImage]() but I'm getting the error Type 'petInfo' does not conform to protocol 'Hashable'. My petInfo struct is this:
struct petInfo {
var petName: String
var dbName: String
}
So I…

MarksCode
- 8,074
- 15
- 64
- 133
42
votes
7 answers
Check for mutability in Python?
Consider this code:
a = {...} # a is an dict with arbitrary contents
b = a.copy()
What role does mutability play in the keys and values of the dicts?
How do I ensure changes to keys or values of one dict are not reflected in the other?
How does…

Matt Joiner
- 112,946
- 110
- 377
- 526
41
votes
5 answers
How can I use a Swift enum as a Dictionary key? (Conforming to Equatable)
I've defined an enum to represent a selection of a "station"; stations are defined by a unique positive integer, so I've created the following enum to allow negative values to represent special selections:
enum StationSelector : Printable {
case…

Doug Knowles
- 873
- 1
- 8
- 14
39
votes
9 answers
Using @functools.lru_cache with dictionary arguments
I have a method that takes (among others) a dictionary as an argument. The method is parsing strings and the dictionary provides replacements for some substrings, so it doesn't have to be mutable.
This function is called quite often, and on…

Evpok
- 4,273
- 3
- 34
- 46
37
votes
7 answers
Conforming to Hashable protocol?
I'm trying to make a dictionary with the key as a struct I've created and the value as an array of Ints. However, I keep getting the error:
Type 'DateStruct' does not conform to protocol 'Hashable'
I'm pretty sure I've implemented the necessary…

MarksCode
- 8,074
- 15
- 64
- 133
29
votes
2 answers
How to Implement hash(into:) from hashValue in Swift?
I don't quite have an idea on what to do with the deprecation warning from the compiler to not use hashValue and instead implement hash(into:).
'Hashable.hashValue' is deprecated as a protocol requirement; conform
type 'MenuItem' to 'Hashable' by…

Glenn Posadas
- 12,555
- 6
- 54
- 95
25
votes
2 answers
Why can't I call hash() on an apparently hashable method of an unhashable instance?
Let's say I have a dictionary:
>>> d = {}
It has a method clear():
>>> d.clear
... which has a __hash__ attribute:
>>> d.clear.__hash__

Zero Piraeus
- 56,143
- 27
- 150
- 160
23
votes
3 answers
Recommended way to implement __eq__ and __hash__
The python documentation mentions that if you override __eq__ and the object is immutable, you should also override __hash__ in order for the class to be properly hashable.
In practice, when I do this I often end up with code like
class…

Jonas Adler
- 10,365
- 5
- 46
- 73
22
votes
1 answer
What is difference between Any , Hashable , AnyHashable in Swift 3?
I scratch my head through lots of tutorials to understand the difference between the above 3 terms and find new term type erased container, now it becomes confusing to me. It raises lots of question.
Why does Swift introduce AnyHashable ?
What is…

technerd
- 14,144
- 10
- 61
- 92
20
votes
4 answers
How to handle hash collisions for Dictionaries in Swift
TLDR
My custom structure implements the Hashable Protocol. However, when hash collisions occur while inserting keys in a Dictionary, they are not automatically handled. How do I overcome this problem?
Background
I had previously asked this question …

Suragch
- 484,302
- 314
- 1,365
- 1,393
18
votes
1 answer
Make a swift protocol conform to Hashable
I'm going around in circles trying to get Hashable to work with multiple struct that conform to the same protocol.
I have a protocol SomeLocation declared like this:
protocol SomeLocation {
var name:String { get }
var coordinates:Coordinate…

Darren
- 10,182
- 20
- 95
- 162
16
votes
2 answers
How to make an enum conform to Hashable with the API available in Xcode 10?
In my Swift 4.2.1 code I have this enumeration:
enum MyEnum {
case caseOne(Int)
case caseTwo(String)
case caseThree
}
It conforms to Equatable:
extension MyEnum: Equatable {
static func == (lhs: MyEnum, rhs: MyEnum) -> Bool {
…

Roman Podymov
- 4,168
- 4
- 30
- 57