Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Learn how to make your code more flexible with packing.
[MUSIC]
0:00
Hi again, welcome to the final stage
of functions, packing and unpacking.
0:04
We've talked a lot about the functions
part of this course so far,
0:09
how are you feeling?
0:12
Do you feel confident in writing basic
functions, passing arguments and
0:14
receiving returns?
0:17
If so, then keep moving forward to
learn about packing and unpacking.
0:19
If not, then before moving on, try to
review to content in the course so far.
0:22
Check out the Treehouse community, and
practice by writing your own functions and
0:27
calling them to see how things work.
0:31
So what's packing?
0:32
Well, in the last section, we talked about
passing multiple arguments to a function,
0:34
and how we receive each of those arguments
into it's own parameter, or variable.
0:39
Packing expands on this concept and let's
us do something potentially more useful,
0:43
and certainly more cool, let's dive in.
0:48
The basic concept of packing is
compressing multiple values into
0:51
one tubal.
0:54
You can do this anywhere
in your Python code,
0:56
but it's very commonly used when passing
multiple arguments to a function.
0:58
Your function receives
the multiple arguments and
1:01
packs them up into a single parameter.
1:04
This parameter now references a tuple
where the elements of the tuple
1:06
are the many arguments passed to
the function, kind of random right?
1:10
Well not so much, there are definitely
going to be times in your python career
1:14
when you want to write a function
that receives data, but
1:18
you're not sure what that data
will look like ahead of time.
1:21
There's also going to be times
when receiving all of your
1:24
arguments into one tuple, cuts down on
the amount of code you need to write, or
1:27
drastically simplifies future steps.
1:31
Let's take a look, for
now just follow along with what I'm doing,
1:33
don't worry about trying
to code along with me.
1:36
So here we have a function called packer
that receives multiple arguments,
1:39
packer prints each argument
onto it's own line.
1:43
This is a pretty basic and
pointless function but humor me,
1:48
we're just using it for
demonstration purposes.
1:50
So I'm gonna write a call to this packer
function where each argument is a string.
1:52
Now I'm gonna run this, just so
2:08
you can get a picture of what
it looks like without packing.
2:09
Each argument has been received
into its own parameter and
2:17
then it's printed out on its own line.
2:19
Now, if we wanted to take
advantage of packing in Python and
2:22
pass these four arguments into one tuple,
2:25
we'd change our function
definition to look more like this.
2:27
We'll get rid of each of these arguments,
2:32
and only use one, and
then we can print args on its own.
2:36
Our function call is gonna
remain exactly the same.
2:44
Now, when we run this, we'll see that each
of our four individual arguments have been
2:48
converted to a tuple and
are printed as such, let's take a look.
2:52
So what did we do here?
3:02
The secret is in the asterisk,
3:04
the little star at the beginning
of the parameter name.
3:06
This star tells Python that we intend to
pack all the arguments sent the function
3:10
into whatever parameter
immediately follows the star.
3:15
This means when packer is called,
and those four arguments are passed,
3:18
the function will receive them all into
one table stored in parameter args.
3:22
args can then be used in the body of the
function, just like any other parameter.
3:26
And because pupils are iterable if we
still wanted each argument print on its
3:31
own line, we can still do that,
but we'll use a loop instead.
3:35
Now I'll save,
I'll come back down to my terminal.
3:50
I'll clear this out to leave
a little more room, okay, so
3:53
when I run the file again, Cool.
3:56
As we iterate over args each element in
the two ball is printed to its own line.
4:00
The parameter args with the asterisk
before it is a very common pattern in
4:04
Python, you'll see the lot.
4:09
Let's dive into args a little more in the
next video with a more practical example.
4:10
You need to sign up for Treehouse in order to download course files.
Sign up