From ab9ee4660ae21c2ac162d2b1e96b4859d2d8bd55 Mon Sep 17 00:00:00 2001 From: Adrien Date: Tue, 7 Jun 2022 07:58:11 +0200 Subject: [PATCH] Add a note about Python functools.singledispatch decorator --- 20211230101535-python.org | 4 +- 20220607074003-functools_singledispatch.org | 72 +++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 20220607074003-functools_singledispatch.org diff --git a/20211230101535-python.org b/20211230101535-python.org index 5045490..9ab1866 100644 --- a/20211230101535-python.org +++ b/20211230101535-python.org @@ -1,6 +1,6 @@ :PROPERTIES: :ID: 4fabfe6a-b104-464f-8a87-dfd7d761dbcc -:mtime: 20220601215830 +:mtime: 20220607075713 :ctime: 20211230101535 :END: #+title: Python @@ -27,6 +27,8 @@ La méthode ~__repr__~ est utilisée pour le debug quand ~__str__~ pour l'utilis ** Fonctions Built-in *** [[id:487f57f5-e9fc-4df7-ae14-b41f9c1fa186][Fonction vars]] *** [[id:acda43fa-70be-4939-9128-47114e48e4cb][Fonction zip]] +** Functools module +*** [[id:25d994fd-375b-429d-af38-6afba818159f][functools.singledispatch]] * Frameworks ** Web diff --git a/20220607074003-functools_singledispatch.org b/20220607074003-functools_singledispatch.org new file mode 100644 index 0000000..2e343e9 --- /dev/null +++ b/20220607074003-functools_singledispatch.org @@ -0,0 +1,72 @@ +:PROPERTIES: +:ID: 25d994fd-375b-429d-af38-6afba818159f +:mtime: 20220607075600 +:ctime: 20220607074003 +:END: +#+title: 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 +#+BEGIN_SRC python :results output +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) = }') +#+END_SRC + +#+RESULTS: +: negator.neg(5) = -5 +: negator.neg(False) = True +** Imbrication (/nested/) avec d'autres /decorator/ +#+BEGIN_SRC python :results output +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) = }') +#+END_SRC + +#+RESULTS: + +* Références + * [[https://medium.com/@sunilrana123/python-trick-functools-singledispatch-4cdf71843cd2][Python Trick : functools.singledispatch - Medium]] + * [[https://docs.python.org/3/library/functools.html#functools.singledispatchmethod][functools.singledispatch - Python]] + * [[https://github.com/python/cpython/issues/83860][functools: singledispatchmethod doesn't work with classmethod #83860 - CPython Github]]