python : programmation orientée-objet · 2020. 10. 15. · python: programmation orientee-objet´...

174
Python : programmation orient ´ ee-objet Achref El Mouelhi Docteur de l’universit ´ e d’Aix-Marseille Chercheur en Programmation par contrainte (IA) Ing ´ enieur en G ´ enie logiciel [email protected] H & H: Research and Training 1 / 117

Upload: others

Post on 28-Mar-2021

37 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

Python : programmation orientee-objet

Achref El Mouelhi

Docteur de l’universite d’Aix-MarseilleChercheur en Programmation par contrainte (IA)

Ingenieur en Genie logiciel

[email protected]

H & H: Research and Training 1 / 117

Page 2: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

Plan

1 Introduction

2 Classestr ()repr ()init ()

SetterGetterproperty()Deleter@propertydel ()

Attributs et methodes statiques

H & H: Research and Training 2 / 117

Page 3: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

Plan

3 HeritageHeritage simpleHeritage multiple

4 PolymorphismeSurchargeRedefinition

5 Classe et methode abstraites

6 Indexeurgetitem ()setitem ()len ()

H & H: Research and Training 3 / 117

Page 4: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

Plan

7 Iterateur

8 Operateureq ()gt ()add ()

9 Genericite

10 Enumeration

11 Introspection

12 Conventions

H & H: Research and Training 4 / 117

Page 5: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Introduction

Python

Qu’est ce qu’une classe en POO?

Ca correspond a un plan, un moule, une usine...

C’est une description abstraite d’un type d’objets

Elle represente un ensemble d’objets ayant les memes proprietesstatiques (attributs) et dynamiques (methodes)

Qu’est ce que c’est la notion d’instance?

Une instance correspond a un objet cree a partir d’une classe (viale constructeur)

L’instanciation : creation d’un objet d’une classe

instance ≡ objet

H & H: Research and Training 5 / 117

Page 6: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Introduction

Python

Qu’est ce qu’une classe en POO?

Ca correspond a un plan, un moule, une usine...

C’est une description abstraite d’un type d’objets

Elle represente un ensemble d’objets ayant les memes proprietesstatiques (attributs) et dynamiques (methodes)

Qu’est ce que c’est la notion d’instance?

Une instance correspond a un objet cree a partir d’une classe (viale constructeur)

L’instanciation : creation d’un objet d’une classe

instance ≡ objet

H & H: Research and Training 5 / 117

Page 7: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Introduction

Python

Qu’est ce qu’une classe en POO?

Ca correspond a un plan, un moule, une usine...

C’est une description abstraite d’un type d’objets

Elle represente un ensemble d’objets ayant les memes proprietesstatiques (attributs) et dynamiques (methodes)

Qu’est ce que c’est la notion d’instance?

Une instance correspond a un objet cree a partir d’une classe (viale constructeur)

L’instanciation : creation d’un objet d’une classe

instance ≡ objet

H & H: Research and Training 5 / 117

Page 8: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Introduction

Python

Qu’est ce qu’une classe en POO?

Ca correspond a un plan, un moule, une usine...

C’est une description abstraite d’un type d’objets

Elle represente un ensemble d’objets ayant les memes proprietesstatiques (attributs) et dynamiques (methodes)

Qu’est ce que c’est la notion d’instance?

Une instance correspond a un objet cree a partir d’une classe (viale constructeur)

L’instanciation : creation d’un objet d’une classe

instance ≡ objet

H & H: Research and Training 5 / 117

Page 9: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Introduction

Python

De quoi est compose une classe?

NomClasse

les attributs

les methodes

Attribut : [visibilite] + type + nom

Methode : [visibilite] + valeur de retour + nom + arguments ≡signature : exactement comme les fonctions en procedurale

H & H: Research and Training 6 / 117

Page 10: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe

Python

Particularite de Python

Toutes les classes heritent implicitement d’une classe mereObject.

Le mot-cle self permet de designer l’objet courant.

H & H: Research and Training 7 / 117

Page 11: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe

Python

Demarche

Utilisez PyCharm pour creer un nouveau projet cours-poo dansvotre espace de travail

Cochez la case qui permet de creer un fichier main.py

H & H: Research and Training 8 / 117

Page 12: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe

PythonConsiderons la classe Personne definie dans personne.py

class Personne:pass

Dans main.py, instancions la classe Personne

p = Personne()

N’oublions d’importer la classe Personne

from personne import Personne

Et si on affiche l’instance

print(p)# affiche <personne.Personne object at 0x00D28160>

H & H: Research and Training 9 / 117

Page 13: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe

PythonConsiderons la classe Personne definie dans personne.py

class Personne:pass

Dans main.py, instancions la classe Personne

p = Personne()

N’oublions d’importer la classe Personne

from personne import Personne

Et si on affiche l’instance

print(p)# affiche <personne.Personne object at 0x00D28160>

H & H: Research and Training 9 / 117

Page 14: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe

PythonConsiderons la classe Personne definie dans personne.py

class Personne:pass

Dans main.py, instancions la classe Personne

p = Personne()

N’oublions d’importer la classe Personne

from personne import Personne

Et si on affiche l’instance

print(p)# affiche <personne.Personne object at 0x00D28160>

H & H: Research and Training 9 / 117

Page 15: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe

PythonConsiderons la classe Personne definie dans personne.py

class Personne:pass

Dans main.py, instancions la classe Personne

p = Personne()

N’oublions d’importer la classe Personne

from personne import Personne

Et si on affiche l’instance

print(p)# affiche <personne.Personne object at 0x00D28160>

H & H: Research and Training 9 / 117

Page 16: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe

PythonDans une classe, on peut declarer des attributs (et les initialiser)

class Personne:num: intnom: strprenom: str

Pour affecter des valeurs aux differents attributs

from personne import Personne

p = Personne()p.num = 100p.nom = ’wick’p.prenom = "john"

Pour afficher la valeur d’un attribut

print(p.nom)# affiche wick

H & H: Research and Training 10 / 117

Page 17: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe

PythonDans une classe, on peut declarer des attributs (et les initialiser)

class Personne:num: intnom: strprenom: str

Pour affecter des valeurs aux differents attributs

from personne import Personne

p = Personne()p.num = 100p.nom = ’wick’p.prenom = "john"

Pour afficher la valeur d’un attribut

print(p.nom)# affiche wick

H & H: Research and Training 10 / 117

Page 18: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe

PythonDans une classe, on peut declarer des attributs (et les initialiser)

class Personne:num: intnom: strprenom: str

Pour affecter des valeurs aux differents attributs

from personne import Personne

p = Personne()p.num = 100p.nom = ’wick’p.prenom = "john"

Pour afficher la valeur d’un attribut

print(p.nom)# affiche wick

H & H: Research and Training 10 / 117

Page 19: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe str ()

Python

Explication

Pour afficher les details d’un objet, il faut que la methode str (self) soit implementee

Definissons la methode str ()

class Personne:num: intnom: strprenom: strdef __str__(self) -> str:

return self.prenom + " " + self.nom

Dans main.py, affichons les details de notre instance p

print(p)# affiche john wick

H & H: Research and Training 11 / 117

Page 20: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe str ()

Python

Explication

Pour afficher les details d’un objet, il faut que la methode str (self) soit implementee

Definissons la methode str ()

class Personne:num: intnom: strprenom: strdef __str__(self) -> str:

return self.prenom + " " + self.nom

Dans main.py, affichons les details de notre instance p

print(p)# affiche john wick

H & H: Research and Training 11 / 117

Page 21: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe str ()

Python

Explication

Pour afficher les details d’un objet, il faut que la methode str (self) soit implementee

Definissons la methode str ()

class Personne:num: intnom: strprenom: strdef __str__(self) -> str:

return self.prenom + " " + self.nom

Dans main.py, affichons les details de notre instance p

print(p)# affiche john wick

H & H: Research and Training 11 / 117

Page 22: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe repr ()

Python

Remplacons la methode str () par repr ()

class Personne:num: intnom: strprenom: strdef __repr__(self) -> str:

return self.prenom + " " + self.nom

Executons le main.py precedent sans le changer

print(p)# affiche john wick

H & H: Research and Training 12 / 117

Page 23: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe repr ()

Python

Remplacons la methode str () par repr ()

class Personne:num: intnom: strprenom: strdef __repr__(self) -> str:

return self.prenom + " " + self.nom

Executons le main.py precedent sans le changer

print(p)# affiche john wick

H & H: Research and Training 12 / 117

Page 24: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe repr ()

Python

Ajoutons la methode str ()

class Personne:num: intnom: strprenom: str

def __str__(self) -> str:return "prenom : " + self.prenom + " " + "nom : " + self.nom

def __repr__(self) -> str:return self.prenom + " " + self.nom

Executons le main.py precedent sans le changer

print(p)# affiche prenom : john nom : wick

H & H: Research and Training 13 / 117

Page 25: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe repr ()

Python

Ajoutons la methode str ()

class Personne:num: intnom: strprenom: str

def __str__(self) -> str:return "prenom : " + self.prenom + " " + "nom : " + self.nom

def __repr__(self) -> str:return self.prenom + " " + self.nom

Executons le main.py precedent sans le changer

print(p)# affiche prenom : john nom : wick

H & H: Research and Training 13 / 117

Page 26: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe repr ()

Python

Testons avec les chaınes formatees

from personne import Personne

p = Personne()p.num = 100p.nom = ’wick’p.prenom = "john"

print(f"{p}")# prenom : john nom : wick

print(f"{p!r}")# john wick

H & H: Research and Training 14 / 117

Page 27: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe repr ()

Python

Convention sur str () et repr ()

Utilisez str () pour un affichage client

Utilisez repr () pour le debogage (phase de developpement)

Plus de details dans la documentation officielle :https://docs.python.org/3/reference/datamodel.html#object. repr

H & H: Research and Training 15 / 117

Page 28: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe init ()

Python

Remarques

Par defaut, toute classe en Python a un constructeur par defautsans parametre

Pour simplifier la creation d’objets, on peut definir un nouveauconstructeur qui prend en parametre plusieurs attributs de laclasse

Les constructeurs avec Python

On le declare avec le mot-cle init ()

Il peut contenir la visibilite des attributs si on veut simplifier ladeclaration

H & H: Research and Training 16 / 117

Page 29: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe init ()

Python

Remarques

Par defaut, toute classe en Python a un constructeur par defautsans parametre

Pour simplifier la creation d’objets, on peut definir un nouveauconstructeur qui prend en parametre plusieurs attributs de laclasse

Les constructeurs avec Python

On le declare avec le mot-cle init ()

Il peut contenir la visibilite des attributs si on veut simplifier ladeclaration

H & H: Research and Training 16 / 117

Page 30: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe init ()

PythonLe constructeur de la classe Personne acceptant trois parametres

class Personne:def __init__(self, num: int, nom: str, prenom: str):

self.num = numself.nom = nomself.prenom = prenom

def __str__(self) -> str:return self.prenom + " " + self.nom

En testant le main precedent, une erreur sera generee

from personne import Personne

p = Personne()p.num = 100p.nom = ’wick’p.prenom = "john"

print(p)# affiche TypeError: __init__() missing 3 required positional arguments

: ’num’, ’nom’, and ’prenom’

H & H: Research and Training 17 / 117

Page 31: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe init ()

PythonLe constructeur de la classe Personne acceptant trois parametres

class Personne:def __init__(self, num: int, nom: str, prenom: str):

self.num = numself.nom = nomself.prenom = prenom

def __str__(self) -> str:return self.prenom + " " + self.nom

En testant le main precedent, une erreur sera generee

from personne import Personne

p = Personne()p.num = 100p.nom = ’wick’p.prenom = "john"

print(p)# affiche TypeError: __init__() missing 3 required positional arguments

: ’num’, ’nom’, and ’prenom’

H & H: Research and Training 17 / 117

Page 32: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe init ()

Python

Explication

Le constructeur par defaut a ete ecrase (il n’existe plus)

Comment faire?

Python n’autorise pas la presence de plusieurs constructeurs (lasurcharge)

On peut utiliser les valeurs par defaut

H & H: Research and Training 18 / 117

Page 33: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe init ()

Python

Explication

Le constructeur par defaut a ete ecrase (il n’existe plus)

Comment faire ?

Python n’autorise pas la presence de plusieurs constructeurs (lasurcharge)

On peut utiliser les valeurs par defaut

H & H: Research and Training 18 / 117

Page 34: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe init ()

Python

Le nouveau constructeur avec les valeurs par defaut

class Personne:

def __init__(self, num: int = 0, nom: str = ’’,prenom: str = ’’):self.num = numself.nom = nomself.prenom = prenom

def __str__(self) -> str:return self.prenom + " " + self.nom

H & H: Research and Training 19 / 117

Page 35: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe init ()

Python

Pour tester les deux constructeurs

from personne import Personne

p = Personne()p.num = 100p.nom = ’wick’p.prenom = "john"

p2 = Personne(100, ’wick’, ’john’)

print(p2)# affiche john wick

H & H: Research and Training 20 / 117

Page 36: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Setter

Python

Hypothese

Supposant que l’on n’accepte pas de valeur negative pour l’attributnum de la classe Personne

Demarche

1 Bloquer l’acces direct aux attributs (mettre la visibilite a private)

2 Definir des methodes qui controlent l’affectation de valeurs auxattributs (les setter)

H & H: Research and Training 21 / 117

Page 37: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Setter

Python

Hypothese

Supposant que l’on n’accepte pas de valeur negative pour l’attributnum de la classe Personne

Demarche

1 Bloquer l’acces direct aux attributs (mettre la visibilite a private)

2 Definir des methodes qui controlent l’affectation de valeurs auxattributs (les setter)

H & H: Research and Training 21 / 117

Page 38: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Setter

Python

Particularite de Python

Le mot-cle private n’existe pas

On prefixe les attributs par deux underscores pour indiquer qu’ilssont prives

On prefixe les attributs par un underscore pour indiquer qu’ils sontproteges

H & H: Research and Training 22 / 117

Page 39: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Setter

Ajoutons deux underscrores a tous les attributs et definissons un setter pour chaqueattribut

class Personne:

def __init__(self, num: int = 0, nom: str = ’’, prenom: str = ’’):self.__num = numself.__nom = nomself.__prenom = prenom

def set_num(self, num: int) -> None:if num > 0:

self.__num = numelse:

self.__num = 0

def set_nom(self, nom: str) -> None:self.__nom = nom

def set_prenom(self, prenom: str) -> None:self.__prenom = prenom

def __str__(self) -> str:return str(self.__num) + " " + self.__prenom + " " + self.__nom

H & H: Research and Training 23 / 117

Page 40: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Setter

Python

Pour tester dans main.py

from personne import Personne

p = Personne(100, ’wick’, ’john’)

print(p)# affiche 100 john wick

p.set_num(-100)

print(p)# affiche 0 john wick

H & H: Research and Training 24 / 117

Page 41: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Setter

Python

Cependant, le constructeur accepte toujours les valeursnegatives

from personne import Personne

p = Personne(-100, ’wick’, ’john’)

print(p)# affiche -100 john wick

p.set_num(-100)

print(p)# affiche 0 john wick

H & H: Research and Training 25 / 117

Page 42: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Setter

Pour resoudre le probleme precedent, on appelle le setter dans le constructeur

class Personne:

def __init__(self, num: int = 0, nom: str = ’’, prenom: str = ’’):self.set_num(num)self.__nom = nomself.__prenom = prenom

def set_num(self, num: int) -> None:if num > 0:

self.__num = numelse:

self.__num = 0

def set_nom(self, nom: str) -> None:self.__nom = nom

def set_prenom(self, prenom: str) -> None:self.__prenom = prenom

def __str__(self) -> str:return str(self.__num) + " " + self.__prenom + " " + self.__nom

H & H: Research and Training 26 / 117

Page 43: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Setter

Python

Les valeurs negatives ne passent plus par le constructeur ni parle setter

from personne import Personne

p = Personne(-100, ’wick’, ’john’)

print(p)# affiche 0 john wick

p.set_num(-100)

print(p)# affiche 0 john wick

H & H: Research and Training 27 / 117

Page 44: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Getter

Python

Question

Comment recuperer les attributs (prives) de la classe Personne?

Demarche

Definir des methodes qui retournent les valeurs des attributs (lesgetter)

H & H: Research and Training 28 / 117

Page 45: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Getter

Python

Question

Comment recuperer les attributs (prives) de la classe Personne?

Demarche

Definir des methodes qui retournent les valeurs des attributs (lesgetter)

H & H: Research and Training 28 / 117

Page 46: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Getter

Ajoutons les getters dans la classe Personne

class Personne:

def __init__(self, num: int = 0, nom: str = ’’, prenom: str = ’’):self.set_num(num)self.__nom = nomself.__prenom = prenom

def get_num(self) -> int:return self.__num

def set_num(self, num: int) -> None:if num > 0:

self.__num = numelse:

self.__num = 0

def get_nom(self) -> str:return self.__nom

def set_nom(self, nom: str) -> None:self.__nom = nom

def get_prenom(self) -> str:return self.__prenom

def set_prenom(self, prenom: str) -> None:self.__prenom = prenom

def __str__(self) -> str:return str(self.__num) + " " + self.__prenom + " " + self.__nom

H & H: Research and Training 29 / 117

Page 47: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Getter

Python

Pour tester

from personne import Personne

p = Personne(100, ’wick’, ’john’)

print(p.get_num(), p.get_nom(), p.get_prenom())# affiche 100 wick john

H & H: Research and Training 30 / 117

Page 48: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe property()

Python

La methode property() permet

d’indiquer les setter et getter

de les utiliser comme un attribut

H & H: Research and Training 31 / 117

Page 49: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe property()

PythonAjoutons property dans la classe Personne

class Personne:def __init__(self, num: int = 0, nom: str = ’’, prenom: str = ’’):

self.num = numself.__nom = nomself.__prenom = prenom

def get_num(self) -> int:return self.__num

def set_num(self, num: int) -> None:if num > 0:

self.__num = numelse:

self.__num = 0num = property(get_num, set_num)

def get_nom(self) -> str:return self.__nom

def set_nom(self, nom: str) -> None:self.__nom = nom

nom = property(get_nom, set_nom)

def get_prenom(self) -> str:return self.__prenom

def set_prenom(self, prenom: str) -> None:self.__prenom = prenom

prenom = property(get_prenom, set_prenom)

def __str__(self) -> str:return str(self.__num) + " " + self.__prenom + " " + self.__nom

H & H: Research and Training 32 / 117

Page 50: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe property()

Python

Pour tester

from personne import Personnep = Personne(100, ’wick’, ’john’)

print(p)# affiche 100 john wick

p.num = -100

print(p.num, p.nom, p.prenom)# affiche 0 wick john

H & H: Research and Training 33 / 117

Page 51: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Deleter

Python

Le deleter permet

de supprimer un attribut d’un objet

de ne plus avoir acces a un attribut

H & H: Research and Training 34 / 117

Page 52: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Deleter

Python

Definissons le deleter pour l’attribut prenom de la classePersonne

def get_prenom(self) -> str:return self.__prenom

def set_prenom(self, prenom: str) -> None:self.__prenom = prenom

def del_prenom(self) -> None:del self.__prenom

prenom = property(get_prenom, set_prenom,del_prenom)

H & H: Research and Training 35 / 117

Page 53: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Deleter

Python

Pour tester

from personne import Personne

p = Personne(100, ’wick’, ’john’)

print(p)# affiche 100 wick john

del p.prenom

print(p)# affiche AttributeError: ’Personne’ object has no

attribute ’__prenom’

H & H: Research and Training 36 / 117

Page 54: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe @property

Python

Le decorateur (annotation) property() permet

de simplifier la declaration des getters et setters

de ne pas declarer les getter et setter avec dans property()

Pour generer les proprietes avec PyCharm

saisissez prop

choisissez si vous voulez le getter, getter et setter, getter, setter etdeleter

validez en cliquant sur entree

H & H: Research and Training 37 / 117

Page 55: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe @property

Python

Le decorateur (annotation) property() permet

de simplifier la declaration des getters et setters

de ne pas declarer les getter et setter avec dans property()

Pour generer les proprietes avec PyCharm

saisissez prop

choisissez si vous voulez le getter, getter et setter, getter, setter etdeleter

validez en cliquant sur entree

H & H: Research and Training 37 / 117

Page 56: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe @property

Modifions la classe Personne et utilisons les decorateurs

class Personne:def __init__(self, num: int = 0, nom: str = ’’, prenom: str = ’’):

self.num = numself.__nom = nomself.__prenom = prenom

@propertydef num(self) -> int:

return [email protected] num(self, num) -> None:

if num > 0:self.__num = num

else:self.__num = 0

@propertydef nom(self) -> str:

return [email protected] nom(self, nom) -> None:

self.__nom = nom@propertydef prenom(self) -> str:

return [email protected] prenom(self, prenom) -> None:

self.__prenom = [email protected] prenom(self) -> str:

del self.__prenomdef __str__(self) -> str:

return str(self.__num) + " " + self.__prenom + " " + self.__nom

H & H: Research and Training 38 / 117

Page 57: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe @property

Python

Pour tester

from personne import Personne

p = Personne(100, ’wick’, ’john’)

print(p)# affiche 100 john wick

p.num = -100

print(p.num, p.nom, p.prenom)# affiche 0 wick john

H & H: Research and Training 39 / 117

Page 58: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe del ()

Python

del ()

destructeur : execute a la destruction de l’objet

peut etre implicitement (lorsque l’objet n’est plus reference) ouexplicitement avec le mot cle del

H & H: Research and Training 40 / 117

Page 59: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe del ()

Python

Ajoutons le destructeur dans la classe Personne

class Personne:

def __init__(self, num: int = 0, nom: str = ’’,prenom: str = ’’):

self.num = numself._nom = nomself._prenom = prenom

def __del__(self):print("destructeur appele")

# + le contenu precedent

H & H: Research and Training 41 / 117

Page 60: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe del ()

Python

Pour tester dans le main

from personne import Personne

p = Personne(100, ’wick’, ’john’)

print(p)# affiche 100 john wick

del p# affiche destructeur appele

H & H: Research and Training 42 / 117

Page 61: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe del ()

Python

Le destructeur sera appele implicitement si l’objet n’est plusreference

from personne import Personne

p = Personne(100, ’wick’, ’john’)

print(p)# affiche 100 john wick

print("fin du programme")# affiche fin du programme# destructeur appele

H & H: Research and Training 43 / 117

Page 62: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Recapitulatif

Les instances d’une meme classe ont toutes les memes attributs mais pas les memesvaleurs

Hypothese

Et si nous voulions qu’un attribut ait une valeur partagee par toutes les instances (parexemple, le nombre d’objets instancies de la classe Personne)

Solution : attribut statique ou attribut de classe

Un attribut dont la valeur est partagee par toutes les instances de la classe.

H & H: Research and Training 44 / 117

Page 63: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Recapitulatif

Les instances d’une meme classe ont toutes les memes attributs mais pas les memesvaleurs

Hypothese

Et si nous voulions qu’un attribut ait une valeur partagee par toutes les instances (parexemple, le nombre d’objets instancies de la classe Personne)

Solution : attribut statique ou attribut de classe

Un attribut dont la valeur est partagee par toutes les instances de la classe.

H & H: Research and Training 44 / 117

Page 64: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Recapitulatif

Les instances d’une meme classe ont toutes les memes attributs mais pas les memesvaleurs

Hypothese

Et si nous voulions qu’un attribut ait une valeur partagee par toutes les instances (parexemple, le nombre d’objets instancies de la classe Personne)

Solution : attribut statique ou attribut de classe

Un attribut dont la valeur est partagee par toutes les instances de la classe.

H & H: Research and Training 44 / 117

Page 65: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Exemple

Si on voulait creer un attribut contenant le nombre d’objets crees a partir de la classePersonne

Notre attribut doit etre declare static, sinon chaque objet pourrait avoir sa propre valeurpour cet attribut

En Python, un attribut qui n’est pas declare dans le constructeur est un attribut static

Particularite de Python

Un attribut qui n’est pas declare dans le constructeur est un attribut statique

Pas de mot-cle static comme dans la plupart des LOO

H & H: Research and Training 45 / 117

Page 66: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Exemple

Si on voulait creer un attribut contenant le nombre d’objets crees a partir de la classePersonne

Notre attribut doit etre declare static, sinon chaque objet pourrait avoir sa propre valeurpour cet attribut

En Python, un attribut qui n’est pas declare dans le constructeur est un attribut static

Particularite de Python

Un attribut qui n’est pas declare dans le constructeur est un attribut statique

Pas de mot-cle static comme dans la plupart des LOO

H & H: Research and Training 45 / 117

Page 67: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Ajoutons un attribut statique nbr personnes a la liste d’attributs de la classe Personne

nbr_personnes = 0

Incrementons notre compteur de personnes dans le constructeur

def __init__(self, num: int = 0, nom: str = ’’, prenom: str = ’’):self.num = numself.__nom = nomself.__prenom = prenomPersonne.nbr_personnes += 1

Decrementons dans le destructeur

def __del__(self):Personne.nbr_personnes -= 1print("destructeur appele")

H & H: Research and Training 46 / 117

Page 68: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Ajoutons un attribut statique nbr personnes a la liste d’attributs de la classe Personne

nbr_personnes = 0

Incrementons notre compteur de personnes dans le constructeur

def __init__(self, num: int = 0, nom: str = ’’, prenom: str = ’’):self.num = numself.__nom = nomself.__prenom = prenomPersonne.nbr_personnes += 1

Decrementons dans le destructeur

def __del__(self):Personne.nbr_personnes -= 1print("destructeur appele")

H & H: Research and Training 46 / 117

Page 69: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Ajoutons un attribut statique nbr personnes a la liste d’attributs de la classe Personne

nbr_personnes = 0

Incrementons notre compteur de personnes dans le constructeur

def __init__(self, num: int = 0, nom: str = ’’, prenom: str = ’’):self.num = numself.__nom = nomself.__prenom = prenomPersonne.nbr_personnes += 1

Decrementons dans le destructeur

def __del__(self):Personne.nbr_personnes -= 1print("destructeur appele")

H & H: Research and Training 46 / 117

Page 70: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Testons cela dans main.py

from personne import Personne

print(Personne.nbr_personnes)# affiche 0

p = Personne(100, ’wick’, ’john’)

print(Personne.nbr_personnes)# affiche 1

H & H: Research and Training 47 / 117

Page 71: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Pour definir une methode statique, on utilise

soit le decorateur staticmethod

soit le decorateur classmethod

H & H: Research and Training 48 / 117

Page 72: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Definissons une methode statique avec le decorateur (@staticmethod)pour incrementer nbr personnes

@staticmethoddef increment() -> None:

Personne.nbr_personnes += 1

Et l’utiliser dans le constructeur

def __init__(self, num: int = 0, nom: str = ’’, prenom:str = ’’):self.num = numself.__nom = nomself.__prenom = prenomPersonne.increment()

H & H: Research and Training 49 / 117

Page 73: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Definissons une methode statique avec le decorateur (@staticmethod)pour incrementer nbr personnes

@staticmethoddef increment() -> None:

Personne.nbr_personnes += 1

Et l’utiliser dans le constructeur

def __init__(self, num: int = 0, nom: str = ’’, prenom:str = ’’):self.num = numself.__nom = nomself.__prenom = prenomPersonne.increment()

H & H: Research and Training 49 / 117

Page 74: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Testons cela dans main.py

from personne import Personne

print(Personne.nbr_personnes)# affiche 0

p = Personne(100, ’wick’, ’john’)

print(Personne.nbr_personnes)# affiche 1

H & H: Research and Training 50 / 117

Page 75: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

On peut aussi definir une methode statique avec le decorateur(@classmethod) et pour incrementer nbr personnes on injecte cls

@classmethoddef increment(cls) -> None:

cls.nbr_personnes += 1

Rien a modifier dans le constructeur

def __init__(self, num: int = 0, nom: str = ’’, prenom:str = ’’):self.num = numself.__nom = nomself.__prenom = prenomPersonne.increment()

H & H: Research and Training 51 / 117

Page 76: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

On peut aussi definir une methode statique avec le decorateur(@classmethod) et pour incrementer nbr personnes on injecte cls

@classmethoddef increment(cls) -> None:

cls.nbr_personnes += 1

Rien a modifier dans le constructeur

def __init__(self, num: int = 0, nom: str = ’’, prenom:str = ’’):self.num = numself.__nom = nomself.__prenom = prenomPersonne.increment()

H & H: Research and Training 51 / 117

Page 77: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Pour tester, rien a modifier dans main.py

from personne import Personne

print(Personne.nbr_personnes)# affiche 0

p = Personne(100, ’wick’, ’john’)

print(Personne.nbr_personnes)# affiche 1

H & H: Research and Training 52 / 117

Page 78: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Pour conclure

Une methode d’instance recoit le mot-cle self comme premierparametre

Une methode de classe recoit le mot-cle cls comme premierparametre

Une methode static ne recoit ni self ni cls comme premierparametre.

H & H: Research and Training 53 / 117

Page 79: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe Attributs et methodes statiques

Python

Exercice

Definissez une classe Adresse avec trois attributs prives rue,code postal et ville de type chaıne de caractere

Definissez un constructeur avec trois parametres, les getters etsetters

Dans la classe Personne, ajouter un attribut adresse (de typeAdresse) dans le constructeur et generez les getter et setter dece nouvel attribut

Dans main.py, creez deux objets : un objet personne (de typePersonne) et adresse (de type Adresse) et affectez le a l’objetpersonne

Affichez tous les attributs de l’objet personne

H & H: Research and Training 54 / 117

Page 80: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage

Python

L’heritage, quand ?

Lorsque deux ou plusieurs classes partagent plusieurs attributs(et methodes)

Lorsqu’une Classe1 est une [sorte de] Classe2

Forme generale

class ClasseFille (ClasseMere):# code

H & H: Research and Training 55 / 117

Page 81: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage

Python

L’heritage, quand ?

Lorsque deux ou plusieurs classes partagent plusieurs attributs(et methodes)

Lorsqu’une Classe1 est une [sorte de] Classe2

Forme generale

class ClasseFille (ClasseMere):# code

H & H: Research and Training 55 / 117

Page 82: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Python

Exemple

Un enseignant a un numero, un nom, un prenom, un genre et un salaire

Un etudiant a aussi un numero, un nom, un prenom, un genre et unniveau

Semantiquement, enseignant et etudiant sont une sorte de personne

En plus, les deux partagent plusieurs attributs tels que numero, nom,prenom et genre

Donc, on peut peut utiliser la classe Personne puisqu’elle contient tousles attributs numero, nom, prenom et genre

Les classes Etudiant et Enseignant heriteront donc de la classePersonne

H & H: Research and Training 56 / 117

Page 83: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Python

Exemple

Un enseignant a un numero, un nom, un prenom, un genre et un salaire

Un etudiant a aussi un numero, un nom, un prenom, un genre et unniveau

Semantiquement, enseignant et etudiant sont une sorte de personne

En plus, les deux partagent plusieurs attributs tels que numero, nom,prenom et genre

Donc, on peut peut utiliser la classe Personne puisqu’elle contient tousles attributs numero, nom, prenom et genre

Les classes Etudiant et Enseignant heriteront donc de la classePersonne

H & H: Research and Training 56 / 117

Page 84: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Python

Exemple

Un enseignant a un numero, un nom, un prenom, un genre et un salaire

Un etudiant a aussi un numero, un nom, un prenom, un genre et unniveau

Semantiquement, enseignant et etudiant sont une sorte de personne

En plus, les deux partagent plusieurs attributs tels que numero, nom,prenom et genre

Donc, on peut peut utiliser la classe Personne puisqu’elle contient tousles attributs numero, nom, prenom et genre

Les classes Etudiant et Enseignant heriteront donc de la classePersonne

H & H: Research and Training 56 / 117

Page 85: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Python

Exemple

Un enseignant a un numero, un nom, un prenom, un genre et un salaire

Un etudiant a aussi un numero, un nom, un prenom, un genre et unniveau

Semantiquement, enseignant et etudiant sont une sorte de personne

En plus, les deux partagent plusieurs attributs tels que numero, nom,prenom et genre

Donc, on peut peut utiliser la classe Personne puisqu’elle contient tousles attributs numero, nom, prenom et genre

Les classes Etudiant et Enseignant heriteront donc de la classePersonne

H & H: Research and Training 56 / 117

Page 86: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Python

Exemple

Un enseignant a un numero, un nom, un prenom, un genre et un salaire

Un etudiant a aussi un numero, un nom, un prenom, un genre et unniveau

Semantiquement, enseignant et etudiant sont une sorte de personne

En plus, les deux partagent plusieurs attributs tels que numero, nom,prenom et genre

Donc, on peut peut utiliser la classe Personne puisqu’elle contient tousles attributs numero, nom, prenom et genre

Les classes Etudiant et Enseignant heriteront donc de la classePersonne

H & H: Research and Training 56 / 117

Page 87: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Python

Exemple

Un enseignant a un numero, un nom, un prenom, un genre et un salaire

Un etudiant a aussi un numero, un nom, un prenom, un genre et unniveau

Semantiquement, enseignant et etudiant sont une sorte de personne

En plus, les deux partagent plusieurs attributs tels que numero, nom,prenom et genre

Donc, on peut peut utiliser la classe Personne puisqu’elle contient tousles attributs numero, nom, prenom et genre

Les classes Etudiant et Enseignant heriteront donc de la classePersonne

H & H: Research and Training 56 / 117

Page 88: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Contenu de etudiant.py

from personne import Personne

class Etudiant(Personne):

def __init__(self, num: int = 0, nom: str = ’’, prenom: str = ’’,niveau: str = ’’):super().__init__(num, nom, prenom)self.__niveau = niveau

@propertydef niveau(self) -> str:

return [email protected] niveau(self, niveau) -> None:

self.__niveau = niveau

Remarques

class A (B): permet d’indiquer que la classe A herite de la classe B

super (): permet d’appeler une methode de la classe mere

H & H: Research and Training 57 / 117

Page 89: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Contenu de etudiant.py

from personne import Personne

class Etudiant(Personne):

def __init__(self, num: int = 0, nom: str = ’’, prenom: str = ’’,niveau: str = ’’):super().__init__(num, nom, prenom)self.__niveau = niveau

@propertydef niveau(self) -> str:

return [email protected] niveau(self, niveau) -> None:

self.__niveau = niveau

Remarques

class A (B): permet d’indiquer que la classe A herite de la classe B

super (): permet d’appeler une methode de la classe mere

H & H: Research and Training 57 / 117

Page 90: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Python

Contenu de enseignant.py

from personne import Personne

class Enseignant(Personne):

def __init__(self, num: int = 0, nom: str = ’’,prenom: str = ’’, salaire: int = 0):super().__init__(num, nom, prenom)self.__salaire = salaire

@propertydef salaire(self) -> int:

return [email protected] salaire(self, salaire) -> None:

self.__salaire = salaire

H & H: Research and Training 58 / 117

Page 91: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Python

Pour creer deux objets Enseignant et Etudiant dans main.py

from etudiant import Etudiantfrom enseignant import Enseignant

etudiant = Etudiant(200, ’maggio’, ’toni’, "licence")print(etudiant)# affiche 200 toni maggio

enseignant = Enseignant(300, ’dalton’, ’jack’, 1700)print(enseignant)# affiche 300 jack dalton

Remarque

Le salaire et le niveau ne sont pas affiches.

H & H: Research and Training 59 / 117

Page 92: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Python

Pour creer deux objets Enseignant et Etudiant dans main.py

from etudiant import Etudiantfrom enseignant import Enseignant

etudiant = Etudiant(200, ’maggio’, ’toni’, "licence")print(etudiant)# affiche 200 toni maggio

enseignant = Enseignant(300, ’dalton’, ’jack’, 1700)print(enseignant)# affiche 300 jack dalton

Remarque

Le salaire et le niveau ne sont pas affiches.

H & H: Research and Training 59 / 117

Page 93: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Python

Redefinissons str dans etudiant.py

def __str__(self):return super().__str__() + " " + self.

__niveau

Et dans enseignant.py

def __str__(self):return super().__str__() + " " + str(self.

__salaire)

H & H: Research and Training 60 / 117

Page 94: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Python

Redefinissons str dans etudiant.py

def __str__(self):return super().__str__() + " " + self.

__niveau

Et dans enseignant.py

def __str__(self):return super().__str__() + " " + str(self.

__salaire)

H & H: Research and Training 60 / 117

Page 95: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Python

Re-testons tout cela dans main.py

from etudiant import Etudiantfrom enseignant import Enseignant

etudiant = Etudiant(200, ’maggio’, ’toni’, "licence")print(etudiant)# affiche 200 toni maggio licence

enseignant = Enseignant(300, ’dalton’, ’jack’, 1700)print(enseignant)# affiche 300 jack dalton 1700

H & H: Research and Training 61 / 117

Page 96: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Python

Remarques

super(Etudiant, self) est une ecriture Python simplifiee etremplacee en Python 3 par super().

En remplacant super() dans Etudiant ou Enseignant parPersonne, le resultat restera le meme.

super() est conseille pour appeler les methodes de la classemere du niveau suivant tant dis que Personne (ou le nom d’uneclasse mere d’un niveau quelconque) permet de preciser leniveau de la classe mere qu’on cherche a appeler.

H & H: Research and Training 62 / 117

Page 97: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

TypeScript

Remarque

Pour connaıtre la classe d’un objet, on peut utiliser le mot-cle isinstance

Exemple

print(isinstance(enseignant, Enseignant))# affiche True

print(isinstance(enseignant, Personne))# affiche True

print(isinstance(p, Enseignant))# affiche False

H & H: Research and Training 63 / 117

Page 98: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

TypeScript

Remarque

Pour connaıtre la classe d’un objet, on peut utiliser le mot-cle isinstance

Exemple

print(isinstance(enseignant, Enseignant))# affiche True

print(isinstance(enseignant, Personne))# affiche True

print(isinstance(p, Enseignant))# affiche False

H & H: Research and Training 63 / 117

Page 99: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Exercice

1 Creer un objet de type Etudiant, un deuxieme de type Enseignant et un dernier detype Personne stocker les tous dans un seul tableau.

2 Parcourir le tableau et afficher pour chacun soit le numero s’il est personne, soit lesalaire s’il est enseignant ou soit le niveau s’il est etudiant.

Pour parcourir un tableau, on peut faire

from personne import Personnefrom etudiant import Etudiantfrom enseignant import Enseignant

p = Personne(100, ’wick’, ’john’)etudiant = Etudiant(200, ’maggio’, ’toni’, "licence")enseignant = Enseignant(300, ’dalton’, ’jack’, 1700)

list = [p, etudiant, enseignant]

for elt in list:pass

H & H: Research and Training 64 / 117

Page 100: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Exercice

1 Creer un objet de type Etudiant, un deuxieme de type Enseignant et un dernier detype Personne stocker les tous dans un seul tableau.

2 Parcourir le tableau et afficher pour chacun soit le numero s’il est personne, soit lesalaire s’il est enseignant ou soit le niveau s’il est etudiant.

Pour parcourir un tableau, on peut faire

from personne import Personnefrom etudiant import Etudiantfrom enseignant import Enseignant

p = Personne(100, ’wick’, ’john’)etudiant = Etudiant(200, ’maggio’, ’toni’, "licence")enseignant = Enseignant(300, ’dalton’, ’jack’, 1700)

list = [p, etudiant, enseignant]

for elt in list:pass

H & H: Research and Training 64 / 117

Page 101: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage simple

Python

Solution

for elt in list:if isinstance(elt, Etudiant):

print(elt.niveau)elif isinstance(elt, Enseignant):

print(elt.salaire)else:

print(elt.num)

H & H: Research and Training 65 / 117

Page 102: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage multiple

Python

Particularite du Python

Heritage multiple : une classe peut heriter simultanement deplusieurs autres

L’heritage multiple est autorise par certains langages commePython, C++...

H & H: Research and Training 66 / 117

Page 103: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage multiple

Considerons la classe Doctorant qui herite de Enseignant et Etudiant

from personne import Personnefrom etudiant import Etudiantfrom enseignant import Enseignant

class Doctorant(Enseignant, Etudiant):

def __init__(self, num: int = 0, nom: str = ’’, prenom: str = ’’,salaire: int = 0, niveau: str = ’’, annee: str = ’’):Enseignant.__init__(self, num, nom, prenom, salaire)Etudiant.__init__(self, num, nom, prenom, niveau)self.__annee = annee

@propertydef annee(self) -> str:

return self.__annee

@annee.setterdef annee(self, annee) -> None:

self.__annee = annee

def __str__(self) -> str:return Personne.__str__(self) + " " + str(self.salaire) + " "

+ self.niveau + " " + self.__annee

H & H: Research and Training 67 / 117

Page 104: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Heritage Heritage multiple

Python

Dans main.py, creons un objet e type Doctorant

from doctorant import Doctorant

doctorant = Doctorant (300, ’cooper’, ’austin’,1700, ’doctorat’, ’1 ere annee’)

print(doctorant)# affiche 300 austin cooper 1700 doctorat 1 ere anne

e

H & H: Research and Training 68 / 117

Page 105: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Polymorphisme Surcharge

Python

Surcharge (overload)

Definir dans une classe plusieurs methodes avec

le meme nom

une signature differente

Si dans une classe, on a deux methodes avec le meme nom,Python remplace toujours la precedente par celle qui est definieapres.

Donc pas de surcharge reelle en Python.

H & H: Research and Training 69 / 117

Page 106: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Polymorphisme Redefinition

Python

Redefinition (override)

Definir une methode dans une classe qui est deja definie dans la classe mere

Possible avec Python

Deux manieres de redefinir une methode

Proposer une nouvelle implementation dans la classe fille differenteet independante de celle de la classe mere (simple comme si ondefinit une nouvelle methode)

Proposer une nouvelle implementation qui fait reference a celle dela classe mere

Exemple

Definir une methode afficherDetails() dans Personne et la redefinir dans Etudiant :une fois sans faire reference a celle de la classe Personne et une autre en faisant reference.

H & H: Research and Training 70 / 117

Page 107: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Polymorphisme Redefinition

Python

Redefinition (override)

Definir une methode dans une classe qui est deja definie dans la classe mere

Possible avec Python

Deux manieres de redefinir une methode

Proposer une nouvelle implementation dans la classe fille differenteet independante de celle de la classe mere (simple comme si ondefinit une nouvelle methode)

Proposer une nouvelle implementation qui fait reference a celle dela classe mere

Exemple

Definir une methode afficherDetails() dans Personne et la redefinir dans Etudiant :une fois sans faire reference a celle de la classe Personne et une autre en faisant reference.

H & H: Research and Training 70 / 117

Page 108: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Polymorphisme Redefinition

Commencons par la definir afficherDetails() dans Personne

def afficher_details(self) -> None:print(self.__prenom + " " + self.__nom)

Ensuite, on la redefinit dans Etudiant

def afficher_details(self) -> None:print(self.__prenom + " " + self.__nom + " " + self.__niveau)

Testons tout cela dans le main)

from personne import Personnefrom etudiant import Etudiant

p = Personne(100, ’wick’, ’john’)p.afficher_details()# affiche john wick

etudiant = Etudiant(200, ’maggio’, ’toni’, "licence")etudiant.afficher_details()# affiche toni maggio licence

H & H: Research and Training 71 / 117

Page 109: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Polymorphisme Redefinition

Commencons par la definir afficherDetails() dans Personne

def afficher_details(self) -> None:print(self.__prenom + " " + self.__nom)

Ensuite, on la redefinit dans Etudiant

def afficher_details(self) -> None:print(self.__prenom + " " + self.__nom + " " + self.__niveau)

Testons tout cela dans le main)

from personne import Personnefrom etudiant import Etudiant

p = Personne(100, ’wick’, ’john’)p.afficher_details()# affiche john wick

etudiant = Etudiant(200, ’maggio’, ’toni’, "licence")etudiant.afficher_details()# affiche toni maggio licence

H & H: Research and Training 71 / 117

Page 110: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Polymorphisme Redefinition

Commencons par la definir afficherDetails() dans Personne

def afficher_details(self) -> None:print(self.__prenom + " " + self.__nom)

Ensuite, on la redefinit dans Etudiant

def afficher_details(self) -> None:print(self.__prenom + " " + self.__nom + " " + self.__niveau)

Testons tout cela dans le main)

from personne import Personnefrom etudiant import Etudiant

p = Personne(100, ’wick’, ’john’)p.afficher_details()# affiche john wick

etudiant = Etudiant(200, ’maggio’, ’toni’, "licence")etudiant.afficher_details()# affiche toni maggio licence

H & H: Research and Training 71 / 117

Page 111: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Polymorphisme Redefinition

Python

On peut aussi utiliser l’implementation de la classe Personne

def afficher_details(self) -> None:Personne.afficher_details(self)print(self.__niveau)

En testant, le resultat est le meme

etudiant = Etudiant(200, ’maggio’, ’toni’, "licence")etudiant.afficher_details()# affiche toni maggiolicence

Refaire la meme chose pour Enseignant

H & H: Research and Training 72 / 117

Page 112: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Polymorphisme Redefinition

Python

On peut aussi utiliser l’implementation de la classe Personne

def afficher_details(self) -> None:Personne.afficher_details(self)print(self.__niveau)

En testant, le resultat est le meme

etudiant = Etudiant(200, ’maggio’, ’toni’, "licence")etudiant.afficher_details()# affiche toni maggiolicence

Refaire la meme chose pour Enseignant

H & H: Research and Training 72 / 117

Page 113: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Polymorphisme Redefinition

Python

On peut aussi utiliser l’implementation de la classe Personne

def afficher_details(self) -> None:Personne.afficher_details(self)print(self.__niveau)

En testant, le resultat est le meme

etudiant = Etudiant(200, ’maggio’, ’toni’, "licence")etudiant.afficher_details()# affiche toni maggiolicence

Refaire la meme chose pour Enseignant

H & H: Research and Training 72 / 117

Page 114: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe et methode abstraites

Python

Classe abstraite

herite de la classe ABC (Abstract Base Class) du module abc

ne peut etre instanciee si elle contient une ou plusieurs methodesabstraites

Si on declare la classe Personne abstraite

from abc import ABC

class Personne(ABC):

# code precedent

H & H: Research and Training 73 / 117

Page 115: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe et methode abstraites

Python

Classe abstraite

herite de la classe ABC (Abstract Base Class) du module abc

ne peut etre instanciee si elle contient une ou plusieurs methodesabstraites

Si on declare la classe Personne abstraite

from abc import ABC

class Personne(ABC):

# code precedent

H & H: Research and Training 73 / 117

Page 116: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe et methode abstraites

Python

Methode abstraite

C’est une methode non implementee (sans code)

Une methode abstraite doit etre declaree dans une classe abstraite

Une methode abstraite doit etre implementee par les classes filles de la classe abstraite

Declarons la methode afficherDetails() comme abstraite dans Personne

@abstractmethoddef afficher_details(self):

pass

Sans oublier d’importer abstractmethod

from abc import ABC, abstractmethod

H & H: Research and Training 74 / 117

Page 117: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe et methode abstraites

Python

Methode abstraite

C’est une methode non implementee (sans code)

Une methode abstraite doit etre declaree dans une classe abstraite

Une methode abstraite doit etre implementee par les classes filles de la classe abstraite

Declarons la methode afficherDetails() comme abstraite dans Personne

@abstractmethoddef afficher_details(self):

pass

Sans oublier d’importer abstractmethod

from abc import ABC, abstractmethod

H & H: Research and Training 74 / 117

Page 118: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe et methode abstraites

Python

Methode abstraite

C’est une methode non implementee (sans code)

Une methode abstraite doit etre declaree dans une classe abstraite

Une methode abstraite doit etre implementee par les classes filles de la classe abstraite

Declarons la methode afficherDetails() comme abstraite dans Personne

@abstractmethoddef afficher_details(self):

pass

Sans oublier d’importer abstractmethod

from abc import ABC, abstractmethod

H & H: Research and Training 74 / 117

Page 119: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Classe et methode abstraites

Python

En testant, le resultat est le meme

etudiant = Etudiant(200, ’maggio’, ’toni’, "licence")

etudiant.afficher_details()# affiche toni maggiolicence

H & H: Research and Training 75 / 117

Page 120: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Indexeur

Python

Definition

Concept utilise en programmation pour faciliter l’acces a untableau d’objet defini dans un objet.

(Autrement dit) Utiliser une ”classe” comme un tableau.

H & H: Research and Training 76 / 117

Page 121: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Indexeur

Python

Considerons la classe ListePersonnes suivante qui contient untableau de Personne

class ListePersonnes:def __init__(self, personnes: list) -> None:

self.__personnes = personnes

Comment faire pour enregistrer des personnes dans le tableaupersonnes et les recuperer facilement en faisant listePersonnes[i]

p = Personne(100, ’wick’, ’john’)p2 = Personne(101, ’dalton’, ’jack’)mes_amis = ListePersonnes([p, p2])print(mes_amis[0])

H & H: Research and Training 77 / 117

Page 122: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Indexeur

Python

Considerons la classe ListePersonnes suivante qui contient untableau de Personne

class ListePersonnes:def __init__(self, personnes: list) -> None:

self.__personnes = personnes

Comment faire pour enregistrer des personnes dans le tableaupersonnes et les recuperer facilement en faisant listePersonnes[i]

p = Personne(100, ’wick’, ’john’)p2 = Personne(101, ’dalton’, ’jack’)mes_amis = ListePersonnes([p, p2])print(mes_amis[0])

H & H: Research and Training 77 / 117

Page 123: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Indexeur getitem ()

Python

Pour pouvoir acceder a un element d’indice i, on ajoute lamethode getitem ()

class ListePersonnes:def __init__(self, personnes: list) -> None:

self.__personnes = personnes

def __getitem__(self, i):return self.__personnes[i]

H & H: Research and Training 78 / 117

Page 124: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Indexeur getitem ()

Python

Dans main.py

from listepersonnes import ListePersonnesfrom personne import Personne

p = Personne(100, ’wick’, ’john’)p2 = Personne(101, ’dalton’, ’jack’)mes_amis = ListePersonnes([p, p2])

print(mes_amis[0])# affiche 100 john wick

H & H: Research and Training 79 / 117

Page 125: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Indexeur setitem ()

Python

Pour pouvoir modifier un element en utilisant son indice, onajoute la methode setitem ()

class ListePersonnes:def __init__(self, personnes: list) -> None:

self.__personnes = personnes

def __getitem__(self, i):return self.__personnes[i]

def __setitem__(self, key, value):self.__personnes[key] = value

H & H: Research and Training 80 / 117

Page 126: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Indexeur setitem ()

Python

Dans main.py

from listepersonnes import ListePersonnesfrom personne import Personne

p = Personne(100, ’wick’, ’john’)p2 = Personne(101, ’dalton’, ’jack’)mes_amis = ListePersonnes([p, p2])mes_amis[1] = Personne(102, ’hadad’, ’karim’)

print(mes_amis[1])# affiche 102 karim hadad

H & H: Research and Training 81 / 117

Page 127: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Indexeur len ()

Python

Pour pouvoir utiliser la fonction len(), on ajoute la methodelen ()

class ListePersonnes:def __init__(self, personnes: list) -> None:

self.__personnes = personnes

def __getitem__(self, i):return self.__personnes[i]

def __setitem__(self, key, value):self.__personnes[key] = value

def __len__(self):return len(self.__personnes)

H & H: Research and Training 82 / 117

Page 128: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Indexeur len ()

Python

Dans main.py

from listepersonnes import ListePersonnesfrom personne import Personne

p = Personne(100, ’wick’, ’john’)p2 = Personne(101, ’dalton’, ’jack’)mes_amis = ListePersonnes([p, p2])

print(len(mes_amis))# affiche 2

H & H: Research and Training 83 / 117

Page 129: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Indexeur len ()

Python

Exercice

Dans Personne, definir un indexeur sur les adresses

Dans le Main, verifier qu’il est possible d’acceder a l’adressed’une personne de ListePersonnes en faisant par exemplemes amis[0][0]

H & H: Research and Training 84 / 117

Page 130: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

Python

Iterateur?

Concept utilise dans plusieurs langages de programmationcomme C++ et Java

Objet utilise pour iterer sur des objets Python iterables comme leslistes, les tuples, les dictionnaires, les ensembles et les chaınesde caracteres

Initialise avec la methode iter ()

Utilisant la methode next () pour recuperer l’element suivant

H & H: Research and Training 85 / 117

Page 131: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

Python

Considerons la liste suivante

liste = [2, 3, 8, 5]

Pour parcourir la liste, plusieurs solutions possibles

utiliser une boucle while ou for

utiliser un iterateur

H & H: Research and Training 86 / 117

Page 132: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

Python

Considerons la liste suivante

liste = [2, 3, 8, 5]

Pour parcourir la liste, plusieurs solutions possibles

utiliser une boucle while ou for

utiliser un iterateur

H & H: Research and Training 86 / 117

Page 133: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

Python

Declarons l’iterateur

iterateur = iter(liste)

Pour demander la premiere valeur

print(iterateur.__next__())# affiche 2

Ou en plus simple

print(next(iterateur))# affiche 5

H & H: Research and Training 87 / 117

Page 134: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

Python

Declarons l’iterateur

iterateur = iter(liste)

Pour demander la premiere valeur

print(iterateur.__next__())# affiche 2

Ou en plus simple

print(next(iterateur))# affiche 5

H & H: Research and Training 87 / 117

Page 135: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

Python

Declarons l’iterateur

iterateur = iter(liste)

Pour demander la premiere valeur

print(iterateur.__next__())# affiche 2

Ou en plus simple

print(next(iterateur))# affiche 5

H & H: Research and Training 87 / 117

Page 136: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

PythonUtilisons une boucle while pour iterer sur tous les elements de la liste

while True:print(next(iterateur))

# affiche# 2# 3# 8# 5# Traceback (most recent call last):# File "C:/Users/elmou/PycharmProjects/cours-poo/main.

py", line 6, in <module># print(next(iterateur))# StopIteration

Une exception sera levee lorsqu’il n’y a plus de valeurs a retourner

H & H: Research and Training 88 / 117

Page 137: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

PythonUtilisons une boucle while pour iterer sur tous les elements de la liste

while True:print(next(iterateur))

# affiche# 2# 3# 8# 5# Traceback (most recent call last):# File "C:/Users/elmou/PycharmProjects/cours-poo/main.

py", line 6, in <module># print(next(iterateur))# StopIteration

Une exception sera levee lorsqu’il n’y a plus de valeurs a retourner

H & H: Research and Training 88 / 117

Page 138: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

Python

Pour resoudre le probleme precedent

while True:try:

print(next(iterateur))except StopIteration:

break# affiche# 2# 3# 8# 5

H & H: Research and Training 89 / 117

Page 139: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

Python

Pour qu’une classe soit iterable, il faut implementer les deuxmethodes iter () et next ()

class Nombre:def __iter__(self):self.valeur = 1return self

def __next__(self):if self.valeur <= 20:x = self.valeurself.valeur += 1return x

else:raise StopIteration

H & H: Research and Training 90 / 117

Page 140: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

Python

La classe Nombre retourne 20 valeurs incrementales commencantpar 1

nombre = Nombre()iter = iter(nombre)

for val in iter:print(val, end=" ")

# affiche 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1718 19 20

H & H: Research and Training 91 / 117

Page 141: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

Python

Pour la suite

Commentez la methode getitem () dans ListePersonnes.

H & H: Research and Training 92 / 117

Page 142: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

Python

Et si on veut parcourir les objets Personne de la classeListePersonnes comme si c’etait une vraie liste, c’est-a-dire :

from listepersonnes import ListePersonnesfrom personne import Personne

p = Personne(100, ’wick’, ’john’)p2 = Personne(101, ’dalton’, ’jack’)mes_amis = ListePersonnes([p, p2])

for personne in mes_amis:print(personne)

Un message d’erreur nous informe que ListePersonnes n’est pasiterable

H & H: Research and Training 93 / 117

Page 143: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

Python

Exercice

Ajoutez les methodes iter () et next () dansListePersonnes pour qu’elle soit iterable.

H & H: Research and Training 94 / 117

Page 144: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Iterateur

Python

Solution

class ListePersonnes:def __init__(self, personnes: list) -> None:

self.__personnes = personnesself.__indice = 0

def __iter__(self):return self

def __next__(self):if self.__indice >= len(self.__personnes):

raise StopIterationelse:

value = self.__personnes[self.__indice]self.__indice += 1return value

H & H: Research and Training 95 / 117

Page 145: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Operateur

Python

Definition

Concept introduit initialement par le langage C++ (surcharged’operateurs)

Permettant de surcharger les operateurs de comparaison ou lesoperateurs arithmetiques deja utilises pour les types predefinis

H & H: Research and Training 96 / 117

Page 146: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Operateur

Python

Operateurs de comparaison et methodes a implementer

== : eq ()

!= : ne ()

>= : ge ()

<= : le ()

> : gt ()

< : lt ()

H & H: Research and Training 97 / 117

Page 147: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Operateur

Python

Operateurs arithmetiques et methodes a implementer

+ : add ()

* : mul ()

** : pow ()

/ : truediv ()

// : floordiv ()

% : mod ()

...

H & H: Research and Training 98 / 117

Page 148: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Operateur

PythonExemple

from personne import Personne

p = Personne(100, ’wick’, ’john’)p2 = Personne(100, ’wick’, ’john’)if p == p2:

print(True)else:

print(False)# affiche False

Remarques

Les deux objets precedents ne sont pas egaux car ils occupent deux espaces memoiresdifferents

Pour definir une nouvelle regle de comparaison d’objets, on doit implementer la methodeeq () dans la classe Personne

H & H: Research and Training 99 / 117

Page 149: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Operateur

PythonExemple

from personne import Personne

p = Personne(100, ’wick’, ’john’)p2 = Personne(100, ’wick’, ’john’)if p == p2:

print(True)else:

print(False)# affiche False

Remarques

Les deux objets precedents ne sont pas egaux car ils occupent deux espaces memoiresdifferents

Pour definir une nouvelle regle de comparaison d’objets, on doit implementer la methodeeq () dans la classe Personne

H & H: Research and Training 99 / 117

Page 150: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Operateur eq ()

Python

Ajoutons l’implementation de la methode eq () dans la classe Personne

def __eq__(self, p: "Personne") -> bool:return p.__nom == self.__nom and p.__prenom == self.

__prenom and p.__num == self.__num

En testant le main precedent, le resultat est

from personne import Personne

p = Personne(100, ’wick’, ’john’)p2 = Personne(100, ’wick’, ’john’)if p == p2:

print(True)else:

print(False)# affiche True

H & H: Research and Training 100 / 117

Page 151: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Operateur eq ()

Python

Ajoutons l’implementation de la methode eq () dans la classe Personne

def __eq__(self, p: "Personne") -> bool:return p.__nom == self.__nom and p.__prenom == self.

__prenom and p.__num == self.__num

En testant le main precedent, le resultat est

from personne import Personne

p = Personne(100, ’wick’, ’john’)p2 = Personne(100, ’wick’, ’john’)if p == p2:

print(True)else:

print(False)# affiche True

H & H: Research and Training 100 / 117

Page 152: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Operateur gt ()

Python

Ajoutons l’implementation de la methode gt () dans la classePersonne

def __gt__(self, other: "Personne"):return self._num > other._num

H & H: Research and Training 101 / 117

Page 153: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Operateur gt ()

Python

testons cela dans le main

from personne import Personne

p = Personne(100, ’wick’, ’john’)p2 = Personne(101, ’wick’, ’john’)

if p2 > p:print(True)

else:print(False)

# affiche True

if p > p2:print(True)

else:print(False)

# affiche False

H & H: Research and Training 102 / 117

Page 154: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Operateur add ()

Python

Ajoutons l’implementation de la methode add () dans la classe Enseignant

def __add__(self, i: int):self.__salaire += ireturn self

testons cela dans main.py

from enseignant import Enseignant

enseignant = Enseignant(300, ’dalton’, ’jack’, 1700)

print(enseignant)# affiche 300 jack dalton 1700

print(enseignant + 200)# affiche 300 jack dalton 1900

H & H: Research and Training 103 / 117

Page 155: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Operateur add ()

Python

Ajoutons l’implementation de la methode add () dans la classe Enseignant

def __add__(self, i: int):self.__salaire += ireturn self

testons cela dans main.py

from enseignant import Enseignant

enseignant = Enseignant(300, ’dalton’, ’jack’, 1700)

print(enseignant)# affiche 300 jack dalton 1700

print(enseignant + 200)# affiche 300 jack dalton 1900

H & H: Research and Training 103 / 117

Page 156: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Genericite

Python

Genericite

Concept connu dans plusieurs LOO (Java, TypeScript, C#)

Objectif : definir des fonctions, classes s’adaptant avec plusieurstypes de donnees

H & H: Research and Training 104 / 117

Page 157: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Genericite

Python

Exemple

si on a besoin d’une classe dont les methodes effectuent lesmemes operations quel que soit le type d’attributs

somme pour entiers ou reels,

concatenation pour chaınes de caracteres,

ou logique pour booleens...

...

Impossible sans definir plusieurs classes (une pour chaquetype)

H & H: Research and Training 105 / 117

Page 158: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Genericite

Python

Solution avec la genericite

from typing import TypeVar, Generic

T = TypeVar(’T’)

class Operation (Generic[T]):

def __init__(self, var1: T, var2: T):self.__var1 = var1self.__var2 = var2

def plus(self):if type(self.__var1) is str :

return self.__var1 + self.__var2elif type(self.__var1) is int and type(self.__var2) is int:

return self.__var1 + self.__var2;elif type(self.__var1) is bool and type(self.__var2) is bool:

return self.__var1 | self.__var2else:

raise TypeError("error")

H & H: Research and Training 106 / 117

Page 159: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Genericite

PythonNous pouvons donc utiliser la meme methode pour plusieurs types differents

from operation import Operation

try:operation1 = Operation[int](5, 3)print(operation1.plus())# affiche 8

operation2 = Operation[str]("bon", "jour")print(operation2.plus())# affiche bonjour

operation3 = Operation[bool](True, False)print(operation3.plus())# affiche true

operation4 = Operation[bool](2.8, 4.9)print(operation4.plus())# affiche probleme de type

except TypeError:print ("probleme de type")

H & H: Research and Training 107 / 117

Page 160: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Enumeration

Python

Enumeration

est un ensemble de noms lies a des valeurs constantes etuniques (nom + valeur = membre)

est iterable

Exemple : definissons l’enumeration Animal

from enum import Enum

Animal = Enum(’Animal’, ’CHAT CHIEN CHAMEAU CHEVALSOURIS’)

H & H: Research and Training 108 / 117

Page 161: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Enumeration

Python

Enumeration

est un ensemble de noms lies a des valeurs constantes etuniques (nom + valeur = membre)

est iterable

Exemple : definissons l’enumeration Animal

from enum import Enum

Animal = Enum(’Animal’, ’CHAT CHIEN CHAMEAU CHEVALSOURIS’)

H & H: Research and Training 108 / 117

Page 162: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Enumeration

Python

Pour acceder a la valeur ou le nom d’un membre d’une enumeration

print (Animal.CHIEN.value)# affiche 2

print (Animal.CHIEN.name)# affiche chien

Pour recuperer un membre, les deux ecritures suivantes sont equivalentes

print (Animal.CHIEN)# affiche Animal.CHIEN

print (Animal[’CHIEN’])# affiche Animal.CHIEN

H & H: Research and Training 109 / 117

Page 163: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Enumeration

Python

Pour acceder a la valeur ou le nom d’un membre d’une enumeration

print (Animal.CHIEN.value)# affiche 2

print (Animal.CHIEN.name)# affiche chien

Pour recuperer un membre, les deux ecritures suivantes sont equivalentes

print (Animal.CHIEN)# affiche Animal.CHIEN

print (Animal[’CHIEN’])# affiche Animal.CHIEN

H & H: Research and Training 109 / 117

Page 164: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Enumeration

Python

Pour recuperer le nom associee a une valeur

print (Animal(1))# affiche Animal.CHAT

Pour iterer sur une enumeration

for animal in Animal:print(animal)

H & H: Research and Training 110 / 117

Page 165: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Enumeration

Python

Pour recuperer le nom associee a une valeur

print (Animal(1))# affiche Animal.CHAT

Pour iterer sur une enumeration

for animal in Animal:print(animal)

H & H: Research and Training 110 / 117

Page 166: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Enumeration

Python

Pour modifier les valeurs par defaut associees aux noms, on declarel’enumeration comme une classe qui herite de Enum

from enum import Enum

class Animal(Enum):CHAT = 1CHIEN = 3CHAMEAU = 2CHEVAL = 4SOURIS = 6

Pour recuperer la valeur associee a un nom (rien ne change)

print(Animal.CHAMEAU.value)# affiche 2

H & H: Research and Training 111 / 117

Page 167: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Enumeration

Python

Pour modifier les valeurs par defaut associees aux noms, on declarel’enumeration comme une classe qui herite de Enum

from enum import Enum

class Animal(Enum):CHAT = 1CHIEN = 3CHAMEAU = 2CHEVAL = 4SOURIS = 6

Pour recuperer la valeur associee a un nom (rien ne change)

print(Animal.CHAMEAU.value)# affiche 2

H & H: Research and Training 111 / 117

Page 168: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Enumeration

Python

Deux noms differents peuvent avoir la meme valeur

from enum import Enum

class Animal(Enum):CHAT = 1CHIEN = 3CHAMEAU = 2CHEVAL = 2SOURIS = 6

En accedant via une valeur, on recupere le premier membre ayant la valeurrecherchee

print(Animal(2))# affiche Animal.CHAMEAU

H & H: Research and Training 112 / 117

Page 169: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Enumeration

Python

Deux noms differents peuvent avoir la meme valeur

from enum import Enum

class Animal(Enum):CHAT = 1CHIEN = 3CHAMEAU = 2CHEVAL = 2SOURIS = 6

En accedant via une valeur, on recupere le premier membre ayant la valeurrecherchee

print(Animal(2))# affiche Animal.CHAMEAU

H & H: Research and Training 112 / 117

Page 170: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Enumeration

Python

Pour forcer l’unicite des valeurs, on utilise le decorateur @unique(ValueError : duplicate values found in <enum ’Animal’> :CHEVAL -> CHAMEAU)

from enum import Enum, unique

@uniqueclass Animal(Enum):

CHAT = 1CHIEN = 3CHAMEAU = 2CHEVAL = 2SOURIS = 6

H & H: Research and Training 113 / 117

Page 171: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Enumeration

Python

Pour affecter des valeurs auto-increment aux differents noms

from enum import Enum, auto

class Animal(Enum):CHAT = auto()CHIEN = auto()CHAMEAU = auto()CHEVAL = auto()SOURIS = auto()

for animal in Animal:print(animal.value)

# affiche 1 2 3 4 5

H & H: Research and Training 114 / 117

Page 172: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Introspection

Python

Fonction dir()

sans parametre : retourne la liste de methodes et attributsdisponibles dans le module

avec parametre (objet ou classe) : retourne la liste de methodes etattributs definis (et herites) de la classe

H & H: Research and Training 115 / 117

Page 173: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Introspection

Python

Quelques constantes sur les fonctions

print(dir())# affiche [’__annotations__’, ’__builtins__’, ’__cached__’, ’

__doc__’, ’__file__’, ’__loader__’, ’__name__’, ’__package__’, ’__spec__’]

print(dir(personne))# affiche [’__class__’, ’__delattr__’, ’__dict__’, ’__dir__’, ’

__doc__’, ’__eq__’, ’__format__’, ’__ge__’, ’__getattribute__’, ’__gt__’, ’__hash__’, ’__init__’, ’__init_subclass__’, ’__le__’, ’__lt__’, ’__module__’, ’__ne__’, ’__new__’, ’__reduce__’, ’__reduce_ex__’, ’__repr__’, ’__setattr__’, ’__sizeof__’, ’__str__’, ’__subclasshook__’, ’__weakref__’, ’increment’, ’nbr_personnes’, ’nom’, ’num’, ’prenom’]

H & H: Research and Training 116 / 117

Page 174: Python : programmation orientée-objet · 2020. 10. 15. · Python: programmation orientee-objet´ Achref El Mouelhi Docteur de l’universite d’Aix-Marseille´ Chercheur en Programmation

c© Achref EL MOUELHI c©

Conventions

Python

Conventions

Une classe par fichier

Deux lignes vides avant et apres la declaration d’une classe

Une ligne vide avant et apres la declaration d’une methode

Un message de documentation pour chaque methode publique aa placer apres la ligne contenant le mot-cle def

Une classe de test par classe

H & H: Research and Training 117 / 117