From 4c1fcce2d6beb7e33ff8de2d6e5358650200d87d Mon Sep 17 00:00:00 2001 From: fbury <florian.bury@cern.ch> Date: Mon, 9 Nov 2020 10:50:41 +0100 Subject: [PATCH] Ajout de subtraction dans la classe complexe --- Chapter_7/complexe.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Chapter_7/complexe.py b/Chapter_7/complexe.py index c1ccf43..c945e82 100644 --- a/Chapter_7/complexe.py +++ b/Chapter_7/complexe.py @@ -16,6 +16,9 @@ class complexe: else: return "{}{}i".format(self.real,self.imag) + def __neg__(self): + return complexe(-self.real,-self.imag) + def __add__(self,other): if isinstance(other,complexe): return complexe(self.real + other.real, self.imag+other.imag) @@ -28,6 +31,18 @@ class complexe: def __radd__(self,other): return self + other + def __sub__(self,other): + if isinstance(other,complexe): + return complexe(self.real - other.real, self.imag-other.imag) + else: + try: + return complexe(self.real-other, self.imag) + except: + return NotImplemented + + def __rsub__(self,other): + return - self + other + def __mul__(self,other): if isinstance(other,complexe): return complexe(self.real*other.real - self.imag * other.imag, @@ -97,6 +112,5 @@ class complexe: else: return (abs(self),math.atan(self.imag/self.real)) - if __name__ == "__main__": print("For explanation on all the 'NotImplemented in the code, please check-out not_implemented_expl.py") -- GitLab