2.1 KiB
2.1 KiB
functools.singledispatch
Introduction
Le module functools permet la définition de single dispatch methods :
- Une sorte de méthode générique pour laquelle différentes implémentations sont écrites,
- L'implémentation à exécuter est déterminée d'après le type du premier argument non self ou non cls,
- Peut être imbriqué (nested) avec d'autres décorateurs mais singledispatchmethod doit être le plus englobant (cf. howto),
- Depuis Python3.8 (3.9 pour les imbrications, cf. PCR 83860 en référence).
Howto
Usage de base
from functools import singledispatchmethod
class Negator:
@singledispatchmethod
def neg(self, arg):
raise NotImplementedError("Cannot negate a")
@neg.register
def _(self, arg: int):
return -arg
@neg.register
def _(self, arg: bool):
return not arg
negator = Negator()
print(f'{negator.neg(5) = }')
print(f'{negator.neg(False) = }')
negator.neg(5) = -5 negator.neg(False) = True
Imbrication (nested) avec d'autres decorator
from functools import singledispatchmethod
class Negator:
@singledispatchmethod
@classmethod
def neg(cls, arg):
raise NotImplementedError("Cannot negate a")
@neg.register
@classmethod
def _(cls, arg: int):
return -arg
@neg.register
@classmethod
def _(cls, arg: bool):
return not arg
print(f'{Negator.neg(5) = }')
print(f'{Negator.neg(False) = }')