.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gyexamples/lambda_function.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gyexamples_lambda_function.py: Astuces avec les lambda functions ================================= Les `lambda `_ fonctions ont :epkg:`Python` sont assez plutôt esthétiques si ce n'est le mot-clé ``lambda`` assez long à écrire. A priori l'écrire est équivalent à celle avec le mot-clé ``def``. On s'en sert parfois aussi pour réduire le nombre d'arguments d'une fonction pour en fixer un. .. GENERATED FROM PYTHON SOURCE LINES 15-32 .. code-block:: default def twoargs(a, b): return a + b def oneargs(x): return twoargs(x, 5) print(oneargs(1)) ################ # Et dans une liste, cela donne ce qui suit. print([oneargs(a) for a in range(0, 3)]) .. GENERATED FROM PYTHON SOURCE LINES 33-35 Dans le cas présent, cela revient à écrire cela ce qui est quand même plus simple. .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: default fcts = [a + 5 for a in range(0, 3)] print(fcts) .. GENERATED FROM PYTHON SOURCE LINES 40-41 Ou encore... .. GENERATED FROM PYTHON SOURCE LINES 41-45 .. code-block:: default fcts = [oneargs(a) for a in range(0, 3)] print(fcts) .. GENERATED FROM PYTHON SOURCE LINES 46-50 Les lambdas fonctions sont aussi utilisées pour retarder l'exécution d'un calcul. La première liste définit le calcul dans des lambda fonctions. La seconde les exécute. .. GENERATED FROM PYTHON SOURCE LINES 50-55 .. code-block:: default fcts_a = [lambda: oneargs(a) for a in range(0, 3)] fcts_b = [f() for f in fcts_a] print(fcts_b) .. GENERATED FROM PYTHON SOURCE LINES 56-64 Le résultat est constant ce qui n'est pas celui souhaité. Les valeurs sont constante. Les fonctions sont exécutées mais l'argument est le même pour tous car elles partagent les mêmes variables locales. Au moment de leur exécution, la variable a ne change plus de valeur. Une solution consiste à conserver chaque valeur distincte de a dans une valeur par défaut. .. GENERATED FROM PYTHON SOURCE LINES 64-69 .. code-block:: default fcts_a = [lambda a=a: oneargs(a) for a in range(0, 3)] fcts_b = [f() for f in fcts_a] print(fcts_b) .. GENERATED FROM PYTHON SOURCE LINES 70-72 :epkg:`pylint` fait surgir le warning suivant quand cela arrive ``W0640: Cell variable v defined in loop``. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_gyexamples_lambda_function.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: lambda_function.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: lambda_function.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_