B1 · Episode 21 · 4:49

Understanding Python Function Execution Step by Step

This episode walks through the step-by-step execution of a Python function, illustrating variable scope, argument passing, memory allocation for local vs. global variables, and how function calls and returns work in practice.

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

Transcript

  1. Donc on peut voir en fait l'intégralité du parcours d'une fonction,

    So we can actually see the entire execution path of a function,

  2. du parcours du programme en le faisant pas à pas avec cet outil- là.

    that is, the program’s step-by-step execution using this tool.

  3. Vous pouvez aller le faire avec Python Tutor directement si vous voulez voir à peu

    You can try it directly on Python Tutor right now if you’d like to see roughly what it looks like,

  4. près ce que ça donne sur Python Tutor.

    to get a sense of how it works.

  5. Voilà le site Python tutor.

    Here is the Python Tutor website.

  6. Vous, ce qui vous intéresse, c'est Python.

    What you need is Python.

  7. Vous pouvez prendre le code qui vous intéresse.

    You can enter the code you’re interested in,

  8. ou que vous êtes en train de faire ou que vous êtes en train de déboguer,

    for example, code you’re currently writing or debugging,

  9. le taper là dedans, le copier coller là dedans.

    or copy and paste it there.

  10. Et on va lui demander de visualiser l'exécution.

    Then ask it to visualize the execution.

  11. On peut lui demander, il a quelques petites options sur la

    It offers several small display options,

  12. manière de l'afficher.

    so you can choose how to view it.

  13. Donc là, on peut par exemple lui demander d'afficher toutes les étapes,

    For instance, we can ask it to show every step,

  14. y compris celles qui étaient faites

    including steps that have already been completed.

  15. passées. Et voilà, et là on retrouve en fait nos différentes étapes,

    And there you go—we see all the different steps again,

  16. avec là ici un affichage un petit peu différent,

    but the display looks slightly different here,

  17. puisqu'il va garder en mémoire les fonctions qui ont été faites dans le passé.

    because it keeps track of functions called earlier.

  18. donc c'est pas exactement le même mode d'affichage que celui que j'ai fait tout à

    So its display mode isn’t exactly the same as the one I showed just now,

  19. l'heure mais c'est la même fonction vous voyez les mêmes étapes donc ça peut être

    but the underlying functionality is identical—you see the same steps,

  20. très pratique pour voir ce qui se passe quand vous faites exécuter votre Python.

    making it very helpful for understanding what happens when your Python code runs.

  21. Donc ça c'était l'exercice 1,

    That was Exercise 1,

  22. on l'a fait ensemble,

    which we did together.

  23. maintenant on peut repasser sur l'exercice 2.

    Now let’s move on to Exercise 2.

  24. Et cette fois, c'est à vous de faire.

    This time, it’s your turn to do it.

  25. Et donc, à chaque fois.

    Each time, it’s the same.

  26. Je vais. Donner l'exo, vous le faites à la main,

    I’ll assign the exercise, and you’ll do it by hand.

  27. enfin on va le faire ensemble après en correction en mode papier,

    Then, we’ll go over and correct it together on paper.

  28. puis ensuite Python Tutor, puis ensuite ExoSur.

    After that, we’ll use Python Tutor, then ExoSur.

  29. Donc vous avez six exos, du plus simple on va dire au plus compliqué,

    You have six exercises, ranging from easiest to most difficult.

  30. le dernier étant particulièrement costaud, on va dire ça comme ça.

    The last one is especially tough—we’ll put it that way.

  31. A few moments later.

    A few moments later.

  32. Alors, pour la correction de cet exercice là, exercice deux.

    Now, let’s correct this exercise—Exercise 2.

  33. Donc on démarre, démarrage du programme, on a une définition de fonction,

    We start running the program: first, a function is defined,

  34. on va directement ici.

    and we go straight here.

  35. Là, on a deux variables qui sont créées, a b égale 3 2,

    Here, two variables are created: a and b, assigned values 3 and 2 respectively.

  36. ça veut dire a vaut 3 et b vaut 2.

    That means a equals 3, and b equals 2.

  37. Donc on va pouvoir écrire.

    So we can write:

  38. Que dans mon programme principal qui est là, j'ai une variable a

    In my main program—which is right here,

  39. qui vaut 3 et une variable b qui vaut 2.

    there’s a variable a equaling 3, and a variable b equaling 2.

  40. Puis je passe à la ligne d'après,

    Then we move to the next line,

  41. et j'ai un appel ici avec un print fonction un a b, a b.

    and here we have a print function call: function_one(a, b), passing in a and b.

  42. Donc, je vais commencer par

    So, I’ll first

  43. remplacer. les valeurs de a et de b par les nombres qui correspondent.

    substitute the values of a and b with their corresponding numbers.

  44. Donc là ici. A vaut 3,

    Here, a is 3,

  45. comme si j'avais enlevé la variable, et puis B vaut 2.

    as if I’d removed the variable name and written the number directly—and b is 2.

  46. Et puis là, mon appel de fonction va se faire en appelant la fonction un avec la

    Then, the function call will execute: calling function one,

  47. valeur de la variable a, 3, et la valeur de b qui vaut 2.

    passing the value of variable a (3) and the value of b (2).

  48. Donc là, mon appel de fonction va être

    So now, my function call becomes:

  49. j'appelle la fonction qui s'appelle fonction un.

    I call the function named function_one.

  50. Et cette fois, j'ai un a

    This time, a equals 3,

  51. qui vaut 3 et un b qui vaut 2.

    and b equals 2.

  52. Ce que vous voyez, c'est que ici j'ai ce A et ce B, et là j'ai ce A et ce B.

    What you see is that here I have this A and this B, and there I have this A and this B.

  53. Ces deux. A et B ne sont pas les mêmes, ces deux cases mémoire,

    These two A’s and B’s are not the same—these two memory locations

  54. le A qui est là et le A qui est là, sont deux cases mémoire différentes.

    are different: the A here and the A there are two distinct memory locations.

  55. Celui- là est créé lorsqu'on appelle la fonction et il disparaîtra lorsqu'on va

    This one is created when the function is called, and

  56. sortir de la fonction.

    it will disappear when we exit the function.

  57. Celui- là, il est en dehors de la fonction, il va rester.

    That one lies outside the function—it will persist.

  58. Pareil pour le B, ce B- là est en dehors de la fonction,

    The same applies to B: this B is outside the function,

  59. celui- là est créé quand on appelle la fonction et c'est tout.

    and that one is created when the function is called—and that’s it.

  60. Donc là, je vais avoir ma commande print A B, donc A qui vaut 3

    So now I’ll run the print(A, B) command—so A equals 3,

  61. et B qui vaut 2, je peux créer mon petit écran qui est là.

    and B equals 2; I can then generate this small screen here.

  62. Ça va m'imprimer 3, 2.

    It will print: 3, 2.

  63. Puis mon opération a plus égale b, donc a.

    Then comes the operation: a += b—so a becomes

  64. est égal à a plus b si je le traduis ça nous fait trois plus deux ça fait cinq donc

    a plus b. Translating that: three plus two equals five—so

  65. a vaut cinq maintenant et je renvoie j'ai oublié de faire mes étapes quand je suis

    a now equals five. I need to return a—but I skipped steps earlier:

  66. passé là je suis passé là j'ai fait mon print

    When I reached here, I jumped straight here and did my print,

  67. et je suis en train de faire

    and now I’m about to compute a plus b.

  68. Je suis là, j'ai fait mon print et je suis en train de faire a plus b.

    Right now, I’ve just finished printing and am computing a plus b.

  69. Là maintenant j'en suis là, return a, donc je vais ressortir.

    Now I’m at this step: return a—so I’m about to exit.