Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Functional Python!
You have completed Functional Python!
Preview
Let's get to some code! We'll look at our data, our datatypes that we'll be using, and talk about what functions as first-class citizens gets us.
Our decorators workshop.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
I said it at the beginning of the course,
but it's worth repeating.
0:00
Functional programming is just
another tool in your shed,
0:03
instead of an entire means to an end.
0:06
In that spirit, we're going to use a bunch
of JSON data and then solve different
0:08
problems with it, instead of just creating
a single unified piece of software.
0:11
Let's go look at the data.
0:15
So before we get into
actually writing some code,
0:17
I wanted to talk about the data that we
are gonna be using, because we need data.
0:19
So the idea is that we're
running a bookseller,
0:25
a bookstore of some sort, and
we have a whole bunch of books and
0:28
we are gonna write software to tell
us information about our books.
0:32
So that's handy.
0:36
And we've got a number of pages,
we have a price, a publishing date,
0:37
it's just a year, a list of subjects,
and a title for each book.
0:42
So you can see here's the book,
11/22/63, here's the book Bag of Bones,
0:48
here's the book Carrie,
here's the book Christine.
0:51
If you didn't guess,
these are all Stephen King books,
0:56
I've got a little bit of a soft spot for
Stephen King.
0:58
I don't know, I find him fun,
he's interesting.
1:00
Okay, so there's that.
1:03
That's all of our data.
1:07
And then you see the prices are these
ridiculously long and precise floats.
1:09
We probably wouldn't want to have that
in actual real data coming from an API.
1:14
But it's fine for this,
we're not gonna let that bother us.
1:19
We're gonna write software that doesn't
care about those ridiculously long floats.
1:23
And we're gonna handle
rounding them all off.
1:28
So we're good, we're fine,
don't worry about those being kinda weird.
1:30
The other thing to talk about
is the software that's going to
1:35
give us our books.
1:37
So you can see here that we
have this class called Book.
1:38
It's a really short class.
1:43
We just take whatever quarks come in,
and assign them as attributes.
1:45
So price will come in and
we'll set that as the price.
1:49
And title will come in and
we'll set that as the title.
1:52
And then we have this get_books
function that takes a file name and
1:54
either gives us raw data, which is just
straight data out of the JSON file,
1:58
just a bunch of dictionaries.
2:03
Or it gives us back our classes.
2:05
So we're gonna set that up as Books and
we're gonna get all of our books.
2:07
We're not gonna need the raw stuff for
the most part.
2:12
We might end up using it once or
twice, but
2:16
for the most part we're
just gonna deal with books.
2:18
But it's kinda nice to have something
like this where if you're like,
2:20
oh I need to be able to get to
the Dictionary, then I can.
2:23
So it's a good idea to keep
something like this in mind.
2:27
And then that's our data.
2:31
We have our data and then we have our
digital, our Python version of the data,
2:34
which is this class here.
2:39
The other thing I wanna talk
about though is functions.
2:40
So we've gone over this in
the Decorator's Workshop,
2:44
which you may or may not have taken.
2:48
And if you didn't I suggest that you do,
you should go check that out.
2:51
But functions are just like
any other variable in Python,
2:54
any other bit of data period.
2:57
A function is the same as an integer
is the same as a dictionary,
2:59
it's the same as a class.
3:04
Which is all fun to mess with.
3:06
And so what that means is that
how we would normally accept and
3:09
integer or a dictionary or
whatever in a function.
3:14
We can accept a function.
3:17
So to illustrate that,
I'm gonna do another function here,
3:19
we're gonna call this say_hello.
3:22
And it's just going to return,
or that's not gonna return,
3:26
sorry it's gonna print, Hello!.
3:31
Okay, and so now, I'm gonna do log_and_run
and I'm gonna pass it say_hello.
3:34
And let's see.
3:41
Let's do a little bit
3:42
of a print log and run.
3:47
And then, I'm not sure why that indented.
3:52
And then we're gonna print log and return.
3:53
And we'll do log_and_ return, say_hello.
3:59
All right so
we're just gonna use our functions here.
4:05
Let's try this out, role this down,
4:08
scroll that up, python functions.py.
4:12
And we get log and run.
4:16
I just got say_hello.
4:18
Hello.
4:19
Log and return.
4:20
I just got say_hello.
4:21
Now why didn't log and
return print out anything?
4:22
Well, it's because it just
returned to the function.
4:25
So test this out.
4:28
Let's do hola = log_and_return and
then we're gonna call hola.
4:30
Okay?
4:37
And look at that.
4:41
We've got effectively
the same thing twice here.
4:42
Now why did that work?
4:45
So the reason that works is because
at the end of log_and_return,
4:47
we returned a function, just like we would
return any other bit of data in Python.
4:50
And here we called the function,
cuz we used these two parens, and
4:55
we called it just like we
would any other function.
4:59
Even though now we don't
know the name of it.
5:01
We just know func.
5:02
So it's really handy to be able
to pass functions around and
5:04
you just think of them
as any other data type.
5:10
They work just like all the others.
5:12
We've worked with functions as
arguments and return values before.
5:15
But I figure it's always good
to have a little refresher.
5:18
If you want a bit more modifying and
5:20
using functions,
check out my Decorator's Workshop.
5:22
I'll link to it in the teacher's notes.
5:25
All right, now that we have usable data.
5:27
Let's get to some of the pieces in
our functional programming tool belt.
5:29
We'll clean up the streets
one function at a time.
5:32
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up