Python: Surcharge: Difference between revisions
Jump to navigation
Jump to search
Wiki-cours (talk | contribs) |
Wiki-cours (talk | contribs) |
||
| Line 126: | Line 126: | ||
{| class="wikitable" border="1" | {| class="wikitable" border="1" | ||
! opération | ! opération | ||
! | ! syntaxe | ||
! méthode | ! méthode | ||
|----- | |----- | ||
Revision as of 17:12, 1 September 2015
Méthodes utiles
| utilisation | nom |
|---|---|
| conversion en string pour print | __str__(self) |
| affichage | __repr__(self) |
Opérations mathématiques
Définir ou redéfinir les opérateurs standards permet d'utiliser les symboles mathématiques pour de nouveaux objets:
| opération | symbole | méthode | symbole unaire | méthode |
|---|---|---|---|---|
| addition | + | __add__(self,other) | += | __radd__(self,other) |
| soustraction | - | __sub__(self,other) | -= | __rsub__(self,other) |
| multiplication | * | __mult__(self,other) | *= | __rmult__(self,other) |
| division | / | __truediv__(self,other) | /= | __rtruediv__(self,other) |
| élévation à la puissance | ** | __pow__(self,other) | **= | __rpow__(self,other) |
| division entière | // | __floordiv__(self,other) | //= | __rfloordiv__(self,other) |
| reste de la division entière (modulo) | % | __mod__(self,other) | %= | __rmod__(self,other) |
| opération | symbole | méthode |
|---|---|---|
| opposé | - | __neg__(self) |
| positif | + | __pos__(self) |
| valeur absolue | abs() | __abs__(self) |
Opérateurs de comparaison
| opération | symbole | méthode |
|---|---|---|
| égal | == | __eq__(self,other) |
| non égal | != ou <> | __ne__(self,other) |
| strictement inférieur | < | __lt__(self,other) |
| strictement supérieur | > | __gt__(self,other) |
| inférieur ou égal | <= | __le__(self,other) |
| supérieur ou égal | >= | __ge__(self,other) |
| comparaison | __cmp__(self,other) |
Opérateurs de conteneurs
destinés à des objets pouvant être des conteneurs
| opération | syntaxe | méthode |
|---|---|---|
| dimension | len(objet) | __len__(self) |
| accès aux éléments en lecture | objet[key] | __getitem__(self,key) |
| accès aux éléments en écriture | objet[key] | __setitem__(self,key) |