Python: Surcharge: Difference between revisions
Jump to navigation
Jump to search
Wiki-cours (talk | contribs) |
Wiki-cours (talk | contribs) |
||
Line 38: | Line 38: | ||
| multiplication | | multiplication | ||
| * | | * | ||
| | | __mul__(self,other) | ||
| *= | | *= | ||
| | | __imul__(self,other) | ||
|-{{ligne grise}} | |-{{ligne grise}} | ||
| division | | division |
Revision as of 22:43, 3 September 2015
Méthodes d'affichage
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) | += | __iadd__(self,other) |
soustraction | - | __sub__(self,other) | -= | __isub__(self,other) |
multiplication | * | __mul__(self,other) | *= | __imul__(self,other) |
division | / | __truediv__(self,other) | /= | __itruediv__(self,other) |
élévation à la puissance | ** | __pow__(self,other) | **= | __ipow__(self,other) |
division entière | // | __floordiv__(self,other) | //= | __ifloordiv__(self,other) |
reste de la division entière (modulo) | % | __mod__(self,other) | %= | __imod__(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,value) |