Complexe

Différentes versions de l'écriture d'une classe pour décrire des nombres complexes...

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python

# version 1: la base
class Complexe(object):
  def __init__(self, x, y):
    self.reel = x
    self.imaginaire = y

  def plus(self, other):
    return Complexe(self.reel+other.reel, self.imaginaire + other.imaginaire)


c1 = Complexe(1,2)
c2 = Complexe(2,3)
print "c1 = (%f,%f)" % (c1.reel, c1.imaginaire)
print "c2 = (%f,%f)" % (c2.reel, c2.imaginaire)
c3 = c1.plus(c2)
print "c3 = (%f,%f)" % (c3.reel, c3.imaginaire)
c3.reel = 4.1
c3.imaginaire = 3.5
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python

# version 2: on protege et on cache
class Complexe(object):
  __slots__ = ["__x", "__y"]
  def __init__(self, x, y):
    self.__x = x
    self.__y = y

  def plus(self, other):
    return Complexe(self.__x+other.__x, self.__y + other.__y)

  def __repr__(self):
    return "(%f,%f)" % (self.__x, self.__y)


c1 = Complexe(1,2)
c2 = Complexe(2,3)
c3 = c1.plus(c2)
print "c1 = %r" % (c1)
print "c2 = %r" % (c2)
print "c3 = %r" % (c3)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python

# version 3: on ajoute accesseurs/mutateurs et on reecrit l'addition
class Complexe(object):
  __slots__ = ["__x", "__y"]
  def __init__(self, x, y):
    self.__x = x
    self.__y = y

  def __add__(self, other):
    return Complexe(self.__x+other.__x, self.__y + other.__y)

  def __repr__(self):
    return "(%f,%f)" % (self.__x, self.__y)

  def __getc(self):
    return (self.__x, self.__y)
  def __setc(self, t): # doit etre un tuple (x,y)
    x, y = t
    self.__x = x
    self.__y = y
  valeur = property(__getc, __setc)


c1 = Complexe(1,2)
c2 = Complexe(2,3)
c3 = c1+c2
print "c1 = %r" % (c1)
print "c2 = %r" % (c2)
print "c3 = %r" % (c3)
c3.valeur = (4.1, 3.5)
print "c3 = %r" % (c3)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python

# version 3: on protege encore plus
class Complexe(object):
  __slots__ = ["__x", "__y"]
  def __init__(self, x, y):
    if not isinstance(x, float):
      raise ValueError("real part should be a float. Here real = %r" % (x))
    if not isinstance(y, float):
      raise ValueError("imaginary part should be a float. Here real = %r" % (y))
    self.__x = x
    self.__y = y

  def __add__(self, other):
    return Complexe(self.__x+other.__x, self.__y + other.__y)

  def __repr__(self):
    return "(%f,%f)" % (self.__x, self.__y)

  def __getc(self):
    return (self.__x, self.__y)
  def __setc(self, t): # doit etre un tuple (x,y)
    if not isinstance(t, tuple):
      raise ValueError("valeur should be a tuple of float. Here valeur = %r" % (t))
    x, y = t
    if not isinstance(x, float):
      raise ValueError("real part of valeur should be a float. Here real = %r" % (x))
    if not isinstance(y, float):
      raise ValueError("imaginary part of valeur should be a float. Here real = %r" % (y))
    self.__x = x
    self.__y = y
  valeur = property(__getc, __setc)


c1 = Complexe(1.,2.)
c2 = Complexe(2.,3.)
c3 = c1+c2
print "c1 = %r" % (c1)
print "c2 = %r" % (c2)
print "c3 = %r" % (c3)
c3.valeur = (4.1, 3.5)
print "c3 = %r" % (c3)