0% found this document useful (0 votes)
39 views3 pages

Class Def Float Float Float Def Return Def Import Return Def Return

The document defines several Python classes: - Point3D class represents 3D points and includes methods for distance calculation, subtraction, and finding nearest neighbors. - Polynome class represents polynomials and includes string representation and evaluation methods. - Cls is a simple class used to demonstrate the __init__ and __del__ methods for object creation and destruction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

Class Def Float Float Float Def Return Def Import Return Def Return

The document defines several Python classes: - Point3D class represents 3D points and includes methods for distance calculation, subtraction, and finding nearest neighbors. - Polynome class represents polynomials and includes string representation and evaluation methods. - Cls is a simple class used to demonstrate the __init__ and __del__ methods for object creation and destruction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

ipynb#

Entrée [38]: class Point3D:


def __init__(self, xm = 0, ym = 0, zm = 0):
self.x = float(xm)
self.y = float(ym)
self.z = float(zm)

def __repr__(self):
motif = "Point3D({},{},{})"
return motif.format(self.x, self.y, self.z)

def __abs__(self):
import math
return math.sqrt(self.x**2 + self.y ** 2 + self.z ** 2)

def __sub__(self, other):


return Point3D(self.x - other.x, self.y - other.y,
self.z - other.z)

def dist2(self, other):


#return self.__sub__(other).__abs__()
#return Point3D.__abs__(Point3D.__sub(self, other))
return abs(self - other)

def plus_proches_voisins(self, pts, n):


l = sorted(pts, key = lambda pt : pt.dist2(self))
#l = sorted(pts, key = lambda pt : self.dist2(pt))
return l[:n]

Entrée [28]: p = Point3D(2,3,4)

Entrée [25]: p # p.__repr__() ==> Point3D.__repr__(p)


Out[25]: Point3D(2.0,3.0,4.0)

Entrée [26]: abs(p) # p.__abs__() == > Point3D.__abs__(p)

Out[26]: 5.385164807134504

Entrée [21]: Point3D(2.0,3.0,4.0)

Out[21]: Point3D(2.0,3.0,4.0)

Entrée [34]: p1 = Point3D()


p2 = Point3D(1,2,1)

Entrée [35]: p1 - p2

Out[35]: Point3D(-1.0,-2.0,-1.0)

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

Out[36]: 2.449489742783178

Entrée [37]: import datetime

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

Entrée [14]: print(d) #print(str(d)) ==> print(d.__str__())

2000-01-01

1 sur 3 24/10/2019 à 10:28


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

Entrée [15]: d # repr(d) ==> d.__repr__()

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

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

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


2000-01-01
^
SyntaxError: invalid token

Entrée [13]: datetime.date(2000, 12, 31)

Out[13]: datetime.date(2000, 12, 31)

Entrée [16]: repr(d)

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

Entrée [17]: str(d)

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

Entrée [39]: p1 = Point3D()


p2 = Point3D(1,2,0)
p3 = Point3D(1,0,0)
p4 = Point3D(10, 10, 1)
p5 = Point3D(-1, -1, -1)
s = {p2 , p3, p4, p5}

Entrée [41]: p1.plus_proches_voisins(s, 200)

Out[41]: [Point3D(1.0,0.0,0.0),
Point3D(-1.0,-1.0,-1.0),
Point3D(1.0,2.0,0.0),
Point3D(10.0,10.0,1.0)]

Entrée [53]: class Polynome:


def __init__(self, lst_coeff):
self.lst_coeff = list(lst_coeff)
def __str__(self):
motif = ""
for deg, coeff in enumerate(self.lst_coeff):
if coeff != 0:
motif += "+ {} X^{}".format(coeff, deg)
return motif

def __call__(self, x):


return sum(coeff * x**deg for deg, coeff in
enumerate(self.lst_coeff))

Entrée [54]: p = Polynome([2, 0, 0, -5, 3])

Entrée [55]: print(p)

+ 2 X^0+ -5 X^3+ 3 X^4

Entrée [56]: p(5) # p.__call__(5) ==> Polynome.__call__(p, 5)

Out[56]: 1252

2 sur 3 24/10/2019 à 10:28


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

Entrée [57]: class Cls:


def __init__(self):
print(" Objet créer ")

def __del__(self):
print(" Objet détruit ")

Entrée [ ]:

3 sur 3 24/10/2019 à 10:28

You might also like