.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/prog/plot_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_auto_examples_prog_plot_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:: Python
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(3)])
.. rst-class:: sphx-glr-script-out
.. code-block:: none
6
[5, 6, 7]
.. 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:: Python
fcts = [a + 5 for a in range(3)]
print(fcts)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
[5, 6, 7]
.. GENERATED FROM PYTHON SOURCE LINES 40-41
Ou encore...
.. GENERATED FROM PYTHON SOURCE LINES 41-45
.. code-block:: Python
fcts = [oneargs(a) for a in range(3)]
print(fcts)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
[5, 6, 7]
.. 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:: Python
fcts_a = [lambda: oneargs(a) for a in range(3)] # noqa: B023
fcts_b = [f() for f in fcts_a]
print(fcts_b)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
[7, 7, 7]
.. 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-68
.. code-block:: Python
fcts_a = [lambda a=a: oneargs(a) for a in range(3)]
fcts_b = [f() for f in fcts_a]
print(fcts_b)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
[5, 6, 7]
.. rst-class:: sphx-glr-timing
**Total running time of the script:** (0 minutes 0.003 seconds)
.. _sphx_glr_download_auto_examples_prog_plot_lambda_function.py:
.. only:: html
.. container:: sphx-glr-footer sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_lambda_function.ipynb `
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_lambda_function.py `
.. container:: sphx-glr-download sphx-glr-download-zip
:download:`Download zipped: plot_lambda_function.zip `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_