From 4ca2751f55de3144d420596cfefdebc61d2f7e6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Mon, 28 Jan 2019 10:52:28 +0100 Subject: [PATCH] Comparable --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d2f30c6..7652150 100644 --- a/README.md +++ b/README.md @@ -635,9 +635,23 @@ class Employee(Person): self.staff_num = staff_num ``` +### Comparable +```python +class MyComparable: + def __init__(self, a): + self.a = a + def __eq__(self, other): + # If not defined it returns id(self) == id(other). + if isinstance(other, MyComparable): + return self.a == other.a + return False + def __hash__(self): + return hash(self.a) +``` + ### Sequence ```python -class : +class MySequence: def __init__(self, seq): self.seq = seq def __len__(self):