- [Narrator] In this video we're going to talk about
functional-style programming in Python.
Now, Python itself is not a functional programming language.
There are languages out there that are `
what we would refer to as
pure functional programming languages.
Uh, Python, on the other hand,
is a general purpose programming language
that supports procedural programming.
It supports object object oriented programming,
and it supports a number of key aspects
of functional programming, ah,
but because it's not an actual
functional programming language we chose to to refer to that
in our book as functional-style programming.
Now, in functional-style programming
we tend to focus on what we want to accomplish
and less on how to accomplish it.
The how part is generally handled by the libraries.
So, just as a for example,
let's revisit that sum function that we've used
a couple of times now that's built into Python.
We can certainly write our own code that specifies
how to calculate the total of a bunch of integer values
and get that final result,
but we don't have to do that
because the sum function already knows how to do that.
So when we want to calculate the sum of a bunch of integers,
we don't have to say, "Go declare a variable called total,
add each individual item to the total,
figure out how many times to do that,
and give me back the total."
All we have to do is say,
"Here is the numbers, calculate the sum."
So it's a more declarative style of programming.
The case where we are figuring out all the details
of how to calculate the total
would be known as external iteration.
We specify not only what we want to accomplish
but how we want to accomplish it.
In functional-style programming,
we focus on internal iteration,
meaning there is still iteration going on,
but it's hidden from us by the libraries,
by things like the sum function
or the methods that are part of the statistics module
that we've demonstrated to you previously as well.
So what you'll find is that
as you get into functional-style programming in Python
you're going to write code where you mostly declare
what it is you want to do.
You will have to specify which library function to use
or which method of a class to use
as we'll see in some later examples,
but you won't have to specify
how those libraries perform their tasks.
They have already done that for you.
Now a key aspect of functional-style programming
is a concept called pure functions.
Pure functions are functions that produce results results
that are dependent only on what you pass into the function
in the first place.
So they don't have any side effects, uh,
where they modify other variables
that you have access to in your code,
and that's what we want to take a look at
here in the context of that sum function once again.
So let's switch over to an IPython session,
and let's create a list of values.
I will just do one, two three for this example.
Now, the sum function is one example
of functional-style programming in the context of Python.
It's a library function, it knows how to perform a task,
the task that it performs is to add up some values,
and give you back a result.
And the result you get back is always going to depend
exclusively on what you give it in the first place.
So if I pass values into sum,
I'm going to get the total six,
and every time I do that I'm going to get the same total
if I give it the same initial values.
Of course, if I give, an and separately it's not going to,
uh, modify the data that it receives.
Therefore, I can see that the values list
still contains one, two, and three,
even after a couple of calls to the sum function.
So a pure function depends,
has outputs that depend only on the inputs,
and there are no side effects as a result
of the function being called,
meaning that the data you give it is left unchanged,
unmodified after the call completes.