Méthodologie de la Programmation
Notes de cours - Séance III
Objets
d = {"Univ" : "Paris 8", "Niveau" : "L1"}
l = [1, 5, 7, 11, 13]
type(d)
type(l)
Création d’une classe
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
point = Point(3, 4)
print(point.x, point.y)
Méthodes spéciales
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return "<" + str(self.x) + "," + str(self.y) + ">"
point = Point(3, 4)
print(point)
Utiliser les classes dans des fonctions
import random
def point_random(xa, ya, xb, yb):
x = random.randint(xa,xb)
y = random.randint(ya, yb)
return Point(x, y)
def point_milieu(Pa, Pb):
x = (Pa.x + Pb.x) // 2
y = (Pa.y + Pb.y) // 2
return Point(x, y)
Ajouter des méthodes à une classe
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return "<" + str(self.x) + "," + str(self.y) + ">"
def distance(self, destination):
pass
point_a = Point(3, 4)
point_b = Point(42, 50)
print(point_a.distance(point_b))
Attention au scope des attributs de classe
class Animal(object):
kind = 'chat'
global variable_globale
def __init__(self, name) :
self.name = name
def changeAnimal(self, newAnimal) :
self.kind = newAnimal
animal_1 = Animal('foo')
animal_2 = Animal('bar')
print(Animal.kind)
print(animal_1.kind)
print(animal_2.kind)
Animal.kind = 'chien'
animal_1.kind = 'cheval'
print(Animal.kind)
print(animal_1.kind)
print(animal_2.kind)
"""
S'ils peuvent porter le même nom,
les attibuts de classe et d'instance sont dans deux scopes distincts.
Les méthodes regardent en priorité les attibuts d'instance
en cas de surcharge des noms.
"""
Alias et copie d’objets
P = Point(3, 4)
Q = P
"""
Ici on crée un alias, un nouveau nom, mais on ne copie pas l'objet.
Mais lorsqu'on déclare Q, aucun nouvel objet n'est crée, celui-ci reçoit
le même pointeur que P.
"""
P.x = 12
print(Q.x)
Q = Point(3, 4)
P = Point(Q.x,Q.y)
"""
Par contre, de cette manière on crée bien une copie.
L'objet Q et l'objet P ont des attributs de même valeurs,
mais sont deux entités distinctes en mémoire.
"""
Classe Fraction
class Fraction(object):
"""
Un nombre représenté sous la forme d'une fraction
"""
def __init__(self, num, denom):
"""
Les numérateurs et dénominateurs sont des entiers.
On verra une autre fois comment contrôler le typage des variables.
"""
self.num = num
self.denom = denom
def __str__(self):
""" Pretty printer """
return str(self.num) + "/" + str(self.denom)
def __add__(self, other):
""" Retourne l'addition de self et d'une Fraction """
top = self.num*other.denom + self.denom*other.num
bott = self.denom*other.denom
return Fraction(top, bott)
def __sub__(self, other):
""" Retourne la soustraction de self et d'une Fraction """
top = self.num*other.denom - self.denom*other.num
bott = self.denom*other.denom
return Fraction(top, bott)
def __mul__(self, other):
""" Retourne la multiplication de self et d'une Fraction """
top = self.num * other.num
bott = self.denom * other.denom
return Fraction(top, bott)
def __float__(self):
""" Cast les valeurs de self en un seul float """
return self.num / self.denom
def __eq__(self, other):
""" Comparateur == """
return float(self) == float(other)
def __neq__(self, other):
""" Comparateur != """
return float(self) != float(other)
def __lt__(self, other):
""" Comparateur < """
return float(self) < float(other)
def __le__(self, other):
""" Comparateur <= """
return float(self) <= float(other)
def __gt__(self, other):
""" Comparateur > """
return float(self) > float(other)
def __ge__(self, other):
""" Comparateur >= """
return float(self) >= float(other)
def inverse(self):
""" Retourne 1 / self """
return Fraction(self.denom, self.num)
fa = Fraction(12,102)
fb = Fraction(1,20)
fc = Fraction(1,1)
print(fa, fb, fc)
print(fa + fb)
print(float(fa), float(fb), float(fc))
print(fc > fa > fb)
print(fa.inverse())
print(fc * Fraction(5,5))
print(fc == Fraction(200,200))