Vous êtes sur la page 1sur 4

POOgrp10 - Jupyter Notebook http://localhost:8888/notebooks/POOgrp10.

ipynb#

Entrée [43]: class Point3D:

def __init__(self, xm = 0, ym = 0, zm = 0):


"""
__init__ est la méthode magique invoquée automatiquement
par Python lors de la création d'une nouvelle
instance de notre classe.
p = Point3D(5,2,3) cet appel engendre
l'appel de Point3D.__init__(p, 5, 2, 3)
"""
self.x = float(xm)
self.y = float(ym)
self.z = float(zm)

def __repr__(self):
"""
__repr__ est une méthode magique qui est responsable
de la représentation formelle des instances de la classe
Point3D. Cette méthode prend toujours un seul paramètre (self)
qui représente l'objet à représenter et doit OBLIGATOIREMENT
renvoyer un str contenant la représentation formelle de self.
"""
return "Point3D({}, {}, {})".format(self.x, self.y, self.z)
#return "Point3D(" + str(self.x) + "," + str(self.y)
# + "," + str(self.z) + ")"

def __abs__(self):
"""
__abs__ est la méthode magique invoquée par Python, lorsque
la fonction native abs est appliquée sur une instance de
la classe courante.
abs(obj) == > type(obj).__abs__(obj)
"""
import math
return math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)

def __sub__(self, other):


"""
__sub__ est la méthode magique invoquée par Python, lorsque
une instance de notre classe figure à gauche de l'opérateur
de soustraction -.
obj1 - obj2 ==>
obj1.__sub__(obj2) ==>
type(obj1).__sub__(obj1, obj2)
"""
return Point3D(self.x - other.x,
self.y - other.y,
self.z - other.z)

def dist2(self, other):


"""
retourne une float contenant la distance Euclidienne
entre self et other
"""
return abs(self - other)
#return Point3D.__abs__(Point3D.__sub__(self, other))

def plus_proches_voisins(self, pts, n):


lst = sorted(pts, key = lambda pt : pt.dist2(self))
#pt.dist2(self) ==> Point3D.dist2(pt, self)

#lst = sorted(pts, key = lambda pt : self.dist2(pt))


return lst[:n]

1 sur 4 24/10/2019 à 12:31


POOgrp10 - Jupyter Notebook http://localhost:8888/notebooks/POOgrp10.ipynb#

Entrée [37]: p1 = Point3D(2,1)

Entrée [38]: p1

Out[38]: Point3D(2.0, 1.0, 0.0)

Entrée [39]: abs(p1)


Point3D.__abs__(p1)
Out[39]: 2.23606797749979

Entrée [40]: p1 = Point3D(5,0,2)


p2 = Point3D(2,0,6)
p1 - p2
Out[40]: Point3D(3.0, 0.0, -4.0)

Entrée [42]: print(p1.dist2(p2))


print(p2.dist2(p1))

5.0
5.0

Entrée [2]: import datetime

Entrée [3]: d = datetime.date(2000, 1, 1)

Entrée [4]: print(d) # print va transformer d en un texte (str)


#print(d) ==> print(str(d))
#str(d) va engendrer l'invoquation de la méthode magique __str__
#str(d) ==> d.__str__() ==> datetime.date.__str__(d)
2000-01-01

Entrée [5]: str(d)

Out[5]: '2000-01-01'

Entrée [6]: datetime.date.__str__(d)

Out[6]: '2000-01-01'

Entrée [7]: 2000-01-01

File "<ipython-input-7-72a33f36f59e>", line 1


2000-01-01
^
SyntaxError: invalid token

str envoie un texte contenant une représentation informelle de l'objet.

Entrée [8]: d

Out[8]: datetime.date(2000, 1, 1)

Entrée [9]: datetime.date(2000, 1, 1)

Out[9]: datetime.date(2000, 1, 1)

2 sur 4 24/10/2019 à 12:31


POOgrp10 - Jupyter Notebook http://localhost:8888/notebooks/POOgrp10.ipynb#

Entrée [11]: repr(d) #repr est une fonction similaire à str


#qui renvoie une représentation textuelle formelle de l'objet
Out[11]: 'datetime.date(2000, 1, 1)'

l'appel repr(d), engendre l'invocation de la méthode magique d.__repr__()

Entrée [12]: d.__repr__()

Out[12]: 'datetime.date(2000, 1, 1)'

Entrée [13]: datetime.date.__repr__(d)

Out[13]: 'datetime.date(2000, 1, 1)'

Entrée [23]: x = -6
print(abs(x)) # résultat de type int
print(x.__abs__())
print(int.__abs__(x))
6
6
6

Entrée [21]: x = -3.14


abs(x) # résultat de type float
Out[21]: 3.14

Entrée [24]: x = 1+1j


print(abs(x)) #résultat de type float
print(complex.__abs__(x))
1.4142135623730951
1.4142135623730951

Entrée [32]: x1 = 3.14


x2 = -2.34
print(x1 - x2)
print(x1.__sub__(x2))
print(float.__sub__(x1, x2))
5.48
5.48
5.48

Entrée [29]: z1 = 1+2j


z2 = 3-4j
z1 -z2
Out[29]: (-2+6j)

Entrée [33]: s1 = {1,2,3,5}


s2 = {1,2,7,8}
print(s1 - s2)
print(set.__sub__(s1, s2))
{3, 5}
{3, 5}

3 sur 4 24/10/2019 à 12:31


POOgrp10 - Jupyter Notebook http://localhost:8888/notebooks/POOgrp10.ipynb#

Entrée [44]: p0 = Point3D()


p1 = Point3D(1,0,0)
p2 = Point3D(0.1, -0.1, 0.3)
p3 = Point3D(6, 0, 6)
p4 = Point3D(6,6,6)
p5 = Point3D(-1, 0, 0)
s = {p1, p2, p3, p4, p5}

Entrée [45]: p0.plus_proches_voisins(s, 3)

Out[45]: [Point3D(0.1, -0.1, 0.3), Point3D(-1.0, 0.0, 0.0), Point3D(1.0, 0.0, 0.0)]

Entrée [ ]:

4 sur 4 24/10/2019 à 12:31

Vous aimerez peut-être aussi