fix: make Delorean hashable by defining __hash__
Defining __eq__ without __hash__ sets __hash__ to None in Python 3, so Delorean instances were unhashable and could not be used in sets or as dict keys (unlike datetime). Hash on self.epoch, the same value __eq__ compares, so equal instances hash equal.
This commit is contained in:
1 parent
c8e576472e
commit
518ab0a093
3 files changed
+87
No files matched your search
@@ -240,6 +240,13 @@ class Delorean(object):
|
||||
return self.epoch == other.epoch
|
||||
return False
|
||||
|
||||
def __hash__(self):
|
||||
# Defining __eq__ sets __hash__ to None in Python 3, which makes
|
||||
# Delorean objects unhashable and unusable in sets or as dict keys.
|
||||
# Hash on the same value equality is based on so that equal objects
|
||||
# (same instant, regardless of timezone) share a hash.
|
||||
return hash(self.epoch)
|
||||
|
||||
def __lt__(self, other):
|
||||
return self.epoch < other.epoch
|
||||
|
||||
|
||||
Reference in new issue
Block a user