Warning: Undefined array key "lang" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
94
Warning: Undefined array key "id" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
117
Warning: Undefined array key "width" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
119
Warning: Undefined array key "dir" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
128
Warning: Undefined array key "lang" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
94
Warning: Undefined array key "id" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
117
Warning: Undefined array key "width" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
119
Warning: Undefined array key "dir" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
128
Warning: Undefined array key "lang" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
94
Warning: Undefined array key "id" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
117
Warning: Undefined array key "width" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
119
Warning: Undefined array key "dir" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
128
Warning: Undefined array key "lang" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
94
Warning: Undefined array key "id" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
117
Warning: Undefined array key "width" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
119
Warning: Undefined array key "dir" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
128
Warning: Undefined array key "lang" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
94
Warning: Undefined array key "id" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
117
Warning: Undefined array key "width" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
119
Warning: Undefined array key "dir" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
128
Warning: Undefined array key "lang" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
94
Warning: Undefined array key "id" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
117
Warning: Undefined array key "width" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
119
Warning: Undefined array key "dir" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
128
Warning: Undefined array key "lang" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
94
Warning: Undefined array key "id" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
117
Warning: Undefined array key "width" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
119
Warning: Undefined array key "dir" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
128
Warning: Undefined array key "lang" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
94
Warning: Undefined array key "id" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
117
Warning: Undefined array key "width" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
119
Warning: Undefined array key "dir" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
128
Warning: Undefined array key "lang" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
94
Warning: Undefined array key "id" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
117
Warning: Undefined array key "width" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
119
Warning: Undefined array key "dir" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
128
Warning: Undefined array key "lang" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
94
Warning: Undefined array key "id" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
117
Warning: Undefined array key "width" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
119
Warning: Undefined array key "dir" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
128
Warning: Undefined array key "lang" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
94
Warning: Undefined array key "id" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
117
Warning: Undefined array key "width" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
119
Warning: Undefined array key "dir" in
/home/signac/doku/lib/plugins/wrap/helper.php on line
128
Programmation orientée objets avec Python
Cette section n'est qu'un ébauche, permettant de se familiariser avec quelques concepts simples de POO
Contrairement à ce que le titre de ce document laisse entendre, l'objet de ce document n'est pas de traiter les différents aspects de la POO, mais plutôt de détailler les différentes notations (et qques concepts) de POO qui sont utiles dans le cadre de l'utilisation du binding PySide (utilisé pour réaliser des interfaces graphiques).
Il s'agit juste d'un bref survol, rapide et absolument pas exhaustif, de quelques éléments utiles à la compréhension du document [[stu:python_gui:pyqt|Interfaces graphiques avec Python et Qt]].
- Des points et des droites
- Une classe Point
Nous désirons manipuler des points du plan, en utilisant la POO.
Pour cela, nous créons une nouvelle classe, qui servira de moule à la création des nouveaux points :
class Point :
pass
Une fois ces lignes entrées, la classe Point
existe est nous pouvons créer de nouveaux objets ainsi :
>>> p=Point()
>>> p
<__main__.Point object at 0x7f0354113a50>
Notre classe ne sert encore à rien. Une des premières choses à faire est de recenser :
Nous devons aussi définir de quelle manière seront créés et initialisés nos objets.
Manifestement, un point du plan est bien représenté par ses coordonnées. Les coordonnées seront les attributs.
Une des premières manipulations nécessaires est l'affichage. Enfin, nos objets seront créés en indiquant leurs coordonnées.
- Méthode spéciale d'initialisation
La méthode spéciale ''__init__ est appelée à l'initialisation des nouveaux objets (pour ceux qui connaissent la POO, c'est presque comme un constructeur, mais pas exactement). Elle prend en paramètre l'objet à initialiser, ainsi que les paramètres qu'on utilise lors de l'initialisation (
x et
y dans notre cas).
En Python, la référence à l'objet est toujours le premier paramètre passé aux méthodes. Par convention, on l'appelle
self :
<code python>
class Point :
def init(self,x,y) :
self.x=x
self.y=y
</code>
Dans le code qui précède : self.x=x
signifie : l'attribut x
de l'objet self
(self
est l'objet que nous initialisons) doit valoir ce que valait x
(le paramètre).
Voyons comment utiliser cet objet :
<code python>
»> p=Point()
Traceback (most recent call last):
File “<pyshell#20>”, line 1, in <module>
p=Point()
TypeError: init() takes exactly 3 arguments (1 given)
</code>
En effet, lors de la création d'un nouvel objet, il faut donner une référence à l'objet (ce qui est fait automatiquement par Python ainsi que 2 entiers (ce qui fait bien au total 3 paramètres) :
<code python>
»> p=Point(2,3)
»> p
<main.Point object at 0x7f8e80113b90>
»> p.x
2
»> p.y
3
»> p.x=42
»> p.x
42
</code>
Nous voyons que les 2 attributs ont bien été initialisés aux valeurs passées lors de la création.
Nous voyons aussi que ces valeurs peuvent être changées sans précaution (c'est un point parfois critiqué de Python).
==== - Une première méthode ====
Nous allons maintenant rajouter une méthode d'affichage :
<code python>
class Point :
def init(self,x,y) :
self.x=x
self.y=y
def affiche(self) :
print(“Point(”,self.x,',',self.y,“)”)
</code>
Voyons comment cela fonctionne :
<code python>
»> p=Point(2,3)
»> p.affiche()
Point( 2 , 3 )
</code>
==== - Une classe Droite ====
Créons maintenant une autre classe, pour représenter des droites du plan, non verticales.
Ces droites sont assimilables aux équations de type y=ax+b. Les attributs représentant nos objets droite seront donc a et
b :
<code python>
class Droite :
def init(self,a,b) :
self.a=a
self.b=b
</code>
Nous pouvons maintenant rajouter une méthode à nos droites, permettant par exemple de savoir si un point est dessus :
<code python>
class Droite :
def init(self,a,b) :
self.a=a
self.b=b
def appartient(self,p) :
if p.y==self.a*p.x+self.b : return True
return False
</code>
Voyons si cela fonctionne :
<code python>
»> d=Droite(2,-3)
»> p1=Point(5,7)
»> p2=Point(5,8)
»> d.appartient(p1)
True
»> d.appartient(p2)
False
»> d.appartient(Point(4,5))
True
</code>
Ajoutons encore une méthode à la classe
Droite permettant, connaissant l'abcisse d'un point, de renvoyer le point de cette droite ayant cette abcisse (il y en a toujours un puisque les droites ne sont pas verticales) :
<code python>
class Droite
…
def prendPoint(self,x) :
y=self.a*x+self.b
return Point(x,y)
</code>
Testons notre nouvelle methode :
<code python>
»> d=Droite(2,3)
»> d=Droite(2,-3)
»> p=d.prendPoint(5)
»> p.affiche()
Point( 5 , 7 )
»> d.prendPoint(5).affiche()
Point( 5 , 7 )
</code>
Inversement, ajoutons à la classe
Point une méthode qui renvoie la droite médiatrice (on fournit le second point en paramètre).
Comme nous avons besoin des coordonnées du milieu, nous ajoutons la méthode qui fait ce travail :
<code python>
def milieu(self,p) :
return Point1)