Welcome back. Now, for this video I want to be a little bit more precise about how
Python evaluates expressions in
the order that Python evaluates different expression partson.
Now, when we're a doing a function call,
the first thing that Python does is it evaluates the function that we're calling itself.
So, it evaluates the function itself.
So, in other words,
let's suppose that we're calling this add function.
The first thing that Python is going to do,
is it's going to look up what is the value of
this expression and it's going to hopefully see that ad as a function.
Now the function call expression might be something more complicated
than just a variable name as long as it evaluates to a function.
Then the next thing that Python is going to do,
is it's going to evaluate each one of the arguments in order.
So, it evaluates the arguments from left to right.
So, in this example,
the first argument is square.
So, Python is going to compute the value of
this expression and then it's going to compute the value of
this expression and then finally after Python knows
what function it's calling and what the value of every argument is,
then Python is going to finally call
the function and then
the value that that function call returns
is going to be the value of this overall expression.
So, let's see that in action.
So, here we assign X to five and Y to seven and the first thing that Python does,
is it evaluates this add function.
So, it sees that adds a function and so the next thing that it does,
is it evaluates each one of its arguments.
So, first, it evaluates this sub-expression and it sees that square itself
is a function and so it's going to repeat
that process and it looks up square and finds that it's a function.
Then it repeats that process, looks up Y,
finds that its value is seven and then it's going to call square on seven and get 49.
Then it's going to look up the value of
this square function and it's going to see that it's
a function it's going to compute the value of each one of its arguments.
So, it's going to see that X is five and then it's going to
call square on five to get 25.
So, far what Python did,
was it found what's the value of ad and then it computed the value of
its two arguments and then it ended up getting 49 as
the first argument and 25 as
the second argument and so now that we have the function itself,
and all of the arguments then we can finally call the function
add on 49 and 25 and as a result,
we're going to get 74 as the value of this overall expression.
Now, suppose that we have this complex expression square of X plus sub square of
Y and two times X and we're asked to
figure out what order is Python actually going to evaluate these arguments in.
Now, bear with me for a bit as I drag each one of these blocks in the correct order.
Okay. So, the first thing that Python is going to do,
is it's going to try to look up the square function and so if we see yep
lookup the variable square to get the function object and then it
needs to compute the value of each one of its arguments.
So, here there's only one argument and so the first thing that Python is
going to do when figuring out what's the value of X plus sub of this function,
is it's first going to look up the value of X because that's leftmost.
So, it's going to be look up the value of variable X.
So, I'm going to get lookup the value of variable X to get two.
Then before we can add X plus this expression,
then we need to compute the value of this expression.
So, when computing the value of this expression,
then we need to in turn lookup the value of the sub-function.
So, we need to look up the variable sub to get
that function object and then we're computing the value of its arguments.
So, the first argument is square of Y.
So, in order to get this,
we need to look up what's the square function.
So, we look up the variable square again to get
the function object and then we need
to compute the value of its argument so we need to look up the value of Y.
So, we'd look up the value of Y to get three and then we back
up and we need to compute the value
of the second argument to sub and in order to do that,
we need to look up X again to get two and we need to multiply two-by-two to get four.
So, now we have the value of both of these expressions.
Oh, excuse me. There's one more thing here.
So, after we look up the value of variable Y to get three,
we actually call the square function.
So, here after we look up Y to get three,
we have to run the square function.
Okay. So, now we have the value of this sub-expression or
this argument and this argument and so now we can finally call this sub function.
So, we run the sub-function on inputs four and nine giving us the value five.
So, then we get X plus five.
So, that's going to give us two plus five and we get
seven and that means that we're calling square with seven as
an argument and so we run the square function
again on the input seven giving us the final value of 49.
That's all for now until next time.