{ "cells": [ { "cell_type": "markdown", "source": [ "# Les fonctions\n", "\n", "Définir des fonctions aide à alléger les programmes mais aussi permet une plus grande flexibilité afin de changer des paramètres, modèles. On peut définir une fonction en python en utilisant: " ], "metadata": {} }, { "cell_type": "code", "execution_count": 1, "source": [ "def myfunc(x,alpha):\n", "\treturn x**alpha" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "Ici la fonction prend deux inputs, une valeur $x$ ainsi qu'un paramètre $\\alpha$. Elle retourne, $f(x) = x^{\\alpha}$. " ], "metadata": {} }, { "cell_type": "code", "execution_count": 2, "source": [ "myfunc(2.0,0.5)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1.4142135623730951" ] }, "metadata": {}, "execution_count": 2 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "On peut définir une valeur par défault pour certains paramètres. Mais tous les paramètres obligatoires doivent apparaître avant ceux qui sont optionnels et qui ont une valeur par défault. Par exemple:" ], "metadata": {} }, { "cell_type": "code", "execution_count": 3, "source": [ "def myfunc(x,alpha=0.5):\n", "\treturn x**alpha" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 4, "source": [ "myfunc(2.0)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1.4142135623730951" ] }, "metadata": {}, "execution_count": 4 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Variables globales et locales\n", "\n", "Les variables définies à l'intérieur d'une fonction sont locales à cette fonction. Elles ne sont pas accessibles en dehors de la fonction. Par exemple, " ], "metadata": {} }, { "cell_type": "code", "execution_count": 5, "source": [ "def myfunc(x,alpha=0.5):\n", "\tbeta = 4.0\n", "\treturn x**alpha" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 6, "source": [ "myfunc(2.0)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1.4142135623730951" ] }, "metadata": {}, "execution_count": 6 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 7, "source": [ "beta" ], "outputs": [ { "output_type": "error", "ename": "NameError", "evalue": "name 'beta' is not defined", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mbeta\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'beta' is not defined" ] } ], "metadata": {} }, { "cell_type": "markdown", "source": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "Que se passe-t-il quand une variable est à la fois assigné localement dans la fonction et existe en globale?" ], "metadata": {} }, { "cell_type": "code", "execution_count": 8, "source": [ "def myfunc(x,alpha=0.5):\n", "\tbeta = 4.0\n", "\treturn x**alpha" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 9, "source": [ "beta = 2.0\n", "myfunc(2.0)\n", "beta" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2.0" ] }, "metadata": {}, "execution_count": 9 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "Sa valeur ne change pas globalement après un changement local dans la fonction. On peut utiliser une variable globale dans une fonction, " ], "metadata": {} }, { "cell_type": "code", "execution_count": 13, "source": [ "def myfunc(x,alpha=0.5):\n", "\treturn x**alpha + beta" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 14, "source": [ "myfunc(2.0)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "3.414213562373095" ] }, "metadata": {}, "execution_count": 14 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "Mais ce n'est pas recommandé afin d'éviter les conflits et faciliter les appels répétés dans lesquels la valeur de `beta` change. Mieux vaut faire " ], "metadata": {} }, { "cell_type": "code", "execution_count": 15, "source": [ "def myfunc(x,beta=0.0,alpha=0.5):\n", "\treturn x**alpha + beta" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 16, "source": [ "myfunc(2.0)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1.4142135623730951" ] }, "metadata": {}, "execution_count": 16 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 17, "source": [ "myfunc(2.0,beta=2.0)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "3.414213562373095" ] }, "metadata": {}, "execution_count": 17 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Fonction partielle\n", "\n", "Certaines fonctions en Python demande comme argument une fonction d'une seule variable. Que faire si notre fonction en a plusieurs. On utilise alors la fonction `partial()` de `functools`:" ], "metadata": {} }, { "cell_type": "code", "execution_count": 18, "source": [ "def myfunc(x,y,alpha,beta):\n", "\treturn x**alpha * y**beta " ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 19, "source": [ "from functools import partial" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 20, "source": [ "pfunc = partial(myfunc,y=1.0,alpha=0.5,beta=1.0)" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 21, "source": [ "pfunc(2.0)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1.4142135623730951" ] }, "metadata": {}, "execution_count": 21 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 22, "source": [ "myfunc(2.0,1.0,0.5,1.0)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1.4142135623730951" ] }, "metadata": {}, "execution_count": 22 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Fonctions à plusieurs résultats\n", "\n", "Que faire si notre fonction retourne plusieurs réponses? On peut retourner ce qu'on appelle un tuple. Voyons voir, " ], "metadata": {} }, { "cell_type": "code", "execution_count": 24, "source": [ "def myfunc(x,y,alpha,beta):\n", "\treturn x**alpha,y**beta" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "Cette fonction retourne deux nombres. " ], "metadata": {} }, { "cell_type": "code", "execution_count": 28, "source": [ "myfunc(2.0,0.0,0.5,1.0)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(1.4142135623730951, 0.0)" ] }, "metadata": {}, "execution_count": 28 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "On peut les assigner avec " ], "metadata": {} }, { "cell_type": "code", "execution_count": 27, "source": [ "xx,yy = myfunc(2.0,0.0,0.5,1.0)\n", "xx" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1.4142135623730951" ] }, "metadata": {}, "execution_count": 27 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Les fonctions `lambda`" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "On peut écrire des fonctions directement sur une ligne. Ceci est utile dans certaines circonstances:" ], "metadata": {} }, { "cell_type": "code", "execution_count": 1, "source": [ "f = lambda x: x**0.5\n", "f(2.0)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1.4142135623730951" ] }, "metadata": {}, "execution_count": 1 } ], "metadata": {} } ], "metadata": { "orig_nbformat": 4, "language_info": { "name": "python", "version": "3.8.5", "mimetype": "text/x-python", "codemirror_mode": { "name": "ipython", "version": 3 }, "pygments_lexer": "ipython3", "nbconvert_exporter": "python", "file_extension": ".py" }, "kernelspec": { "name": "python3", "display_name": "Python 3.8.5 64-bit ('base': conda)" }, "interpreter": { "hash": "ba2340ab882356406e091df0706039b4b3cc5191eef6c073d3fb97005dbe0324" } }, "nbformat": 4, "nbformat_minor": 2 }