Python: Surcharge: Difference between revisions
Jump to navigation
Jump to search
Wiki-cours (talk | contribs) |
Wiki-cours (talk | contribs) |
||
Line 28: | Line 28: | ||
| __add__(self,other) | | __add__(self,other) | ||
| += | | += | ||
| | | __iadd__(self,other) | ||
|-{{ligne grise}} | |-{{ligne grise}} | ||
| soustraction | | soustraction | ||
Line 34: | Line 34: | ||
| __sub__(self,other) | | __sub__(self,other) | ||
| -= | | -= | ||
| | | __isub__(self,other) | ||
|----- | |----- | ||
| multiplication | | multiplication | ||
Line 40: | Line 40: | ||
| __mult__(self,other) | | __mult__(self,other) | ||
| *= | | *= | ||
| | | __imult__(self,other) | ||
|-{{ligne grise}} | |-{{ligne grise}} | ||
| division | | division | ||
Line 46: | Line 46: | ||
| __truediv__(self,other) | | __truediv__(self,other) | ||
| /= | | /= | ||
| | | __itruediv__(self,other) | ||
|----- | |----- | ||
| élévation à la puissance | | élévation à la puissance | ||
Line 52: | Line 52: | ||
| __pow__(self,other) | | __pow__(self,other) | ||
| **= | | **= | ||
| | | __ipow__(self,other) | ||
|----- | |----- | ||
| division entière | | division entière | ||
Line 58: | Line 58: | ||
| __floordiv__(self,other) | | __floordiv__(self,other) | ||
| //= | | //= | ||
| | | __ifloordiv__(self,other) | ||
|-{{ligne grise}} | |-{{ligne grise}} | ||
| reste de la division entière (modulo) | | reste de la division entière (modulo) | ||
Line 64: | Line 64: | ||
| __mod__(self,other) | | __mod__(self,other) | ||
| %= | | %= | ||
| | | __imod__(self,other) | ||
|} | |} | ||
Revision as of 21:59, 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 | * | __mult__(self,other) | *= | __imult__(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) |