Questions tagged [hashable]

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.

144 questions
115
votes
11 answers

Why can't I use a list as a dict key in python?

I'm a bit confused about what can/can't be used as a key for a python dict. dicked = {} dicked[None] = 'foo' # None ok dicked[(1,3)] = 'baz' # tuple ok import sys dicked[sys] = 'bar' # wow, even a module is ok ! dicked[(1,[3])] = 'qux'…
wim
  • 266,989
  • 79
  • 484
  • 630
46
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
  • 40,904
  • 40
  • 170
  • 328
40
votes
1 answer

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
  • 68,602
  • 40
  • 230
  • 241
36
votes
4 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
  • 813
  • 1
  • 8
  • 14
35
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
  • 5,456
  • 12
  • 42
  • 95
32
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
  • 100,604
  • 94
  • 332
  • 495
29
votes
7 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
  • 3,635
  • 1
  • 33
  • 44
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
  • 47,176
  • 24
  • 135
  • 148
20
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
  • 12,929
  • 9
  • 56
  • 82
16
votes
6 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
  • 5,456
  • 12
  • 42
  • 95
16
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
  • 364,799
  • 232
  • 1,155
  • 1,198
15
votes
2 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
  • 8,636
  • 2
  • 35
  • 71
14
votes
3 answers

How does Dictionary use the Equatable protocol in Swift?

In order to solve this question, I have been playing around with a custom struct that implements the Hashable Protocol. I'm trying to see how many times the equivalency operator overload (==) gets called depending on if there is a hash collision or…
Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198
12
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
  • 10,706
  • 4
  • 36
  • 75
11
votes
2 answers

Creating a protocol that represents hashable objects that can be on or off

I'm trying to create a simple protocol that says whether or not an object is in an "on" state or an "off" state. The interpretation of what that is depends on the implementing object. For a UISwitch, it's whether the switch is on or off (duh). For a…
Tim Fuqua
  • 1,316
  • 14
  • 21
1
2 3
9 10