B1 · Episode 20 · 4:54

Tracing Python Function Execution Step by Step

This episode walks through the step-by-step execution of a Python function, showing how function calls work, how arguments (including default ones) are passed, how local variables change during execution, and how return values replace function calls in the main program.

Source:jmbarbier / peertube.solidev.net · License:CC BY-SA 4.0

Transcript

  1. Puis mon programme continue, donc nouvel appel de la fonction 2,

    Then my program continues, so function2 is called again,

  2. je retourne là- haut, et cette fois je vais à nouveau.

    I return to the top, and this time I execute again.

  3. écrire l'appel de la fonction, donc nouvel appel de fonction2.

    This time, I write the function call again—so another call to function2.

  4. Cette fois l'appel il se fait avec a qui

    This time, the call uses a with value

  5. vaut 2, a qui vaut 2, et puis b n'est pas donné ici, il n'y a pas de b.

    2; a equals 2, and b is not provided here—there’s no b.

  6. Donc b va prendre la valeur par défaut, b vaut 3.

    So b takes its default value: b equals 3.

  7. Donc qu'est- ce que je fais à l'intérieur de ma fonction?

    So what do I do inside my function?

  8. Je fais b est égal à a moins b.

    I set b equal to a minus b.

  9. Et ça nous donne 2 moins 3,

    That gives us 2 minus 3,

  10. c'est à dire moins 1.

    which is negative 1.

  11. Donc b contient maintenant la valeur moins 1.

    So b now holds the value negative 1.

  12. A n'a pas changé, return.

    a hasn’t changed—then we return.

  13. étape d'après, return B.

    Next step: return b.

  14. Je reviens à mon programme à plan.

    I return to the main program.

  15. Donc je reviens avec cette fois une variable qui revient à moins un,

    This time, I return with a variable whose value is negative 1,

  16. une valeur qui revient à moins un.

    a returned value of negative 1.

  17. Donc ma fonction est maintenant remplacée par la valeur de retour,

    So my function is now replaced by its return value,

  18. et cette valeur de retour, elle fait- 1.

    and that return value is negative 1.

  19. Donc j'ai ici la nouvelle valeur qui va s'afficher ici- 1.

    So the new value displayed here is negative 1.

  20. Donc mon programme, je l'ai parcouru en fait l'exécution de mon programme,

    So I’ve actually traced through the execution of my program,

  21. et à chaque fois que j'ai une fonction qui est appelée,

    and each time a function is called,

  22. je précise quelle fonction a été appelée, avec quels paramètres.

    I specify which function was called and with which parameters.

  23. Quelle fonction a été appelée, le nom de la fonction,

    Which function was called—the function’s name?

  24. avec quels paramètres. Ensuite, je vais pouvoir à l'intérieur

    With which parameters? Then, I can examine inside,

  25. regarder l'évolution des valeurs des différentes variables.

    how the values of different variables change.

  26. Là, b a changé de valeur.

    Here, the value of b has changed.

  27. Et puis, quand on sort de la fonction qu'on a un return, on revient en arrière,

    Then, when we hit a return statement in the function, we go back up,

  28. on revient à l'endroit où on était avant.

    to where we were before the call.

  29. Pareil, deuxième appel de fonction.

    Likewise, this is the second function call.

  30. Quelle fonction a été appelée?

    Which function was called?

  31. Je suis dans la fonction, je regarde les variables de la fonction,

    I enter the function and examine its local variables,

  32. et je reviens.

    and then return.

  33. à mon programme à plan.

    back to my main program.

  34. Et à chaque fois qu'une fonction a été appelée, quand on revient,

    Each time a function is called and we return,

  35. en fait, c'est ce qui a été fait ici, la.

    what’s actually happening here is exactly this.

  36. L'appel de la fonction est remplacé par la valeur de la fonction.

    the function call is replaced by its returned value.

  37. Donc. On peut aussi voir ça,

    So, we can also understand it this way:

  38. on peut vérifier d'abord qu'on s'est pas planté, en utilisant.

    First, verify we haven’t made a mistake using the notebook for this exercise.

  39. le notebook correspondant à l'exercice, donc on le fait en papier d'abord,

    We do it on paper first,

  40. puis après on va aller vérifier.

    then go check the result.

  41. Donc je peux exécuter ma fonction.

    So I can now run my function.

  42. Et on a bien ici un 2- 1, ce qui correspond à ce qu'on avait prévu.

    Here we correctly see 2 minus 1, matching what we expected.

  43. Et juste en dessous, on a ici en fait le programme dans un site internet qui

    Just below is our program running on a website,

  44. s'appelle Python Tutor, sur lequel on peut aller directement,

    called Python Tutor, which you can visit directly,

  45. là je l'ai inclus à l'intérieur,

    and I’ve embedded it here,

  46. où vous pouvez taper votre programme et aller voir l'exécution de votre programme

    where you can type in your own program and step through its execution.

  47. pas à pas. Donc là en fait, les différentes étapes qu'on va voir quand on va faire

    So, as we click 'Next' repeatedly,

  48. suivant, suivant, suivant, sont exactement les mêmes étapes

    each step we see,

  49. que celles qu'on a fait à la main quand j'ai fait en fait ce passage- là.

    exactly matches the steps I drew manually just now.

  50. Donc on va, il nous dit qu'il y a 11 étapes, d'accord, on ne va pas être loin.

    It says there are 11 steps — okay, we won’t go far.

  51. 1, 2, 3, 4, 5, 6, 7, 8, 9.

    1, 2, 3, 4, 5, 6, 7, 8, 9.

  52. dix et onze en fait à peu près donc on a à peu près sur le même nombre d'étapes et

    Then ten and eleven—so we have roughly the same number of steps,

  53. on va pouvoir visualiser ça en même temps qu'on visualise la mémoire correspondante

    and we can visualize the corresponding memory state at the same time.

  54. à chacun. des endroits où on est.

    That is, at each location where we currently are.

  55. Donc si on reprend.

    So let’s start over.

  56. Ici, on va regarder.

    Here, we’ll take a look.

  57. Première étape, le Python voit l'existence de la fonction.

    Step one: Python detects the existence of this function.

  58. Très bien, il va jusqu'à la fin, il connaît l'existence de la fonction.

    Great—it reads all the way to the end and thus learns that this function exists.

  59. Et là, vous voyez dans l'état de la mémoire,

    Now, you see in the memory state,

  60. il a quelque part maintenant la connaissance d'une fonction qui s'appelle fonction2,

    it now holds knowledge of a function named fonction2,

  61. qui prend deux arguments, dont un qui s'appelle b, qui vaut 3,

    which takes two arguments—one called b, with value 3,

  62. avec un argument par défaut.

    and another with a default value.

  63. Puis on arrive, on est maintenant à cette ligne- là, print fonction2 3 1.

    Then we reach this line: print(fonction2(3, 1)).

  64. Là, on va rentrer.

    At this point, we’ll enter the function call.

  65. dans l'appel de la fonction deux avec les arguments trois un et c'est ce qui me fait

    That is, calling fonction2 with arguments 3 and 1—and this causes,

  66. ici il me dit ok voilà je suis dans la fonction deux et les arguments les variables

    here, it tells me: “OK, I’m now inside fonction2,”

  67. que j'ai disponibles sont trois et un.

    “and the variables available to me are 3 and 1.”

  68. Puis b est égal à moins b, j'exécute, ça nous fait b égal 2,

    Then b = -b executes, giving us b = 2,

  69. return b, et je vais avoir ici le retour.

    then return b runs, and the return value appears here.

  70. La valeur de renvoi, c'est 2.

    The return value is 2.

  71. Et je suis revenu dans mon programme principal.

    Then we return to the main program.

  72. Et vous voyez que l'affichage a été fait ici.

    And you see the output appears right here.

  73. Puis nouvel appel de ma fonction2, cette fois avec un appel avec a qui vaut 2,

    Next, we call fonction2 again—this time with a equal to 2,

  74. et puis b qui n'était pas donné, donc qui prend la valeur 3.

    and b not provided, so it takes the default value 3.

  75. Et là dans ce cas- là, je vais afficher b égale à a moins b.

    In this case, I’ll print the result of b = a - b.

  76. Le b est calculé moins un, je renvoie la valeur b,

    Variable b is calculated as minus one, and I return the value of b,

  77. donc la valeur de retour c'est b,

    so the function's return value is b,

  78. donc l'appel de la fonction qui a été fait sera remplacé par la valeur de retour,

    meaning that this function call will be replaced by its return value,

  79. ça me fait un print moins un et mon programme est terminé.

    thus executing print minus one, and my program ends.