From 4c6d7360ce64173bbaeb185613281c11473667be Mon Sep 17 00:00:00 2001 From: kratorr <31060213+kratorr@users.noreply.github.com> Date: Mon, 12 Jul 2021 12:20:33 +0300 Subject: [PATCH] Add answer (#136) Co-authored-by: Vladimir --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index e5e8e9b..4dace9f 100644 --- a/README.md +++ b/README.md @@ -8370,6 +8370,26 @@ a = f()
You wrote a class to represent a car. How would you compare two cars instances if two cars are equal if they have the same model and color?
+ +``` +class Car: + def __init__(self, model, color): + self.model = model + self.color = color + + def __eq__(self, other): + if not isinstance(other, Car): + return NotImplemented + return self.model == other.model and self.color == other.color + +>> a = Car('model_1', 'red') +>> b = Car('model_2', 'green') +>> c = Car('model_1', 'red') +>> a == b +False +>> a == c +True +```