Python for Absolute Beginners Transcripts
Chapter: The big ideas of software development
Lecture: Code execution diagram

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So, we have our source code that we're going to talk about, but not yet. And it's written into this file called urlify.py.
0:08 To run it, we just say, Python, here's some Python code. Run this little application. And we type this on our command prompt. What happens next?
0:16 Well, obviously, we start out with the urlify.py file. This is the Python source code. And this is what most people think of
0:23 as the thing that is running maybe right at the beginning. But in fact, this is not what is run. This is transformed several times
0:31 into something else that then is actually run by Python. So this is parsed or compiled by Python.
0:39 Basically, Python goes through and understands all the words and symbols and stuff in the source file and it converts it into a lower level thing that
0:49 it doesn't have to try to interpret how you've written it. It knows now there's an if statement. Here's the thing we're testing, test it.
0:56 Stuff like that, like very simple, low level byte code that Python can use. Now this step is not hugely expensive but it's not for free
1:04 and you're reading potentially very large and complicated text files and turning them into this form that's been validated
1:11 and there's a canonical representation of all the stuff in there. So what happens next is this is actually saved
1:18 into a local folder called double __pycache. And the reason it does this is the next time you run this file second, third, and fourth time you run it
1:28 Python can skip that first step. And it goes, this file corresponds to this Python byte code. We're just going to start from there and run it
1:35 as a way to make sure it hasn't changed and things like that. But it'll just grab this pre read pre understood version
1:43 and start running that. What does it do with this? Well, it fires up the Python runtime or virtual machine. Sometimes it's called a Python interpreter.
1:52 It's going to take this Python byte code and feed it to there and say Okay, what I want you to do is go through this step by step
1:59 getting the data wherever you need it to get from. Does it come off a file? Do you ask the user? Do you get it from a web service?
2:05 And use that to help make decisions and choose which byte instruction, bytecode piece to run next? Usually, when you write some Python code
2:13 like if this thing is greater than if the age is less than 14 that might turn into three or four different
2:19 little instructions to make that test actually happen. What Python going to do is it's going to take that byte code
2:26 and it's going to feed it into a loop that just goes around and around and around and around, basically, endlessly
2:32 as long as there's more byte code to be run. And it's going to break it up into step by step by step
2:36 and says, Okay, this step is to load up a value, age. This step is to load up another value 14. The next step is to test are the two loaded values
2:45 less than equal less than each other. Is the first one less than the second one. And it just goes through things like that. So it has this byte code
2:53 it's going to break it up into little pieces. Some of those might say we need to talk to the file system and go pull in some data from there.
3:01 To open a file stream and we're going to start working on that. Some of it might create some memory structure that lives in RAM.
3:08 And it's going to read from it and write from it. And that'll help it keep track of what it's doing. So their code might say
3:13 I also need to read from that thing let me go up there and see, you know what the name of the logged in user is, for example.
3:19 There's other stuff that happens to. We don't have to explicitly say I need to create this place in RAM to put the data and then now I'm done with it.
3:27 Some languages actually do. But Python automatically cleans it up. So there's two levels of what is called garbage collection
3:34 that just goes and throws away the data returns it back to the operating system. If you're done using it. Though, maybe I created the name of somebody
3:41 got the name of the user, put it into HTML then I'm done with that variable. Well, something has to give that memory back to the computer.
3:48 So there's some processes called garbage collection that take care of that. This is only a small part of what happens when you run your Python code.
3:57 Code is also turned into this thing called a tree. So that it can be understood in a more generalized way. There's a lot of stuff going on.
4:06 But this is a pretty good mental model to have. When you type Python my source code file what is happening, this step from left to right
4:14 and that loop and loop and loop again and again and again. Figure out what each byte code instruction says to do.
4:21 This is what happens when we run our code.


Talk Python's Mastodon Michael Kennedy's Mastodon