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