- [Narrator] In this video
I'd like to talk more
about passing objects to functions.
Now you may recall back in the preceding lesson
that we talked about the fact
that every single object in Python
is passed to a function by reference.
And the reason for that is,
in Python everything is an object,
and the way you manipulate objects is through references
to those objects,
not by having direct access
to the objects themselves.
So when I pass something like a list to a function,
because a list is modifiable
and because the function is receiving
what is in fact the location of that list in memory,
the function can talk directly
to the list object
through that reference
and modify the contents of the list.
So I'm going to paste a snippet
into this session here
for a function called Modify Elements
which is going to multiply
all of the elements in Items,
its parameter by two,
and we want to use this
to demonstrate the fact that
when pass a list into a function,
the function can modify the list.
So we're going to iterate
through every index representing
the element positions
within the list called Items
and for each of those items,
we're going to multiple its value by two
and store the result back into the element
within the original list,
wherever that thing happens to be in memory.
So let's go ahead define that function
and, of course, we need some values to work with
so let's create a numbers list
and we'll give it some values.
Let's do like 5 values here
and let's evaluate numbers
just so we can see it's contents.
And, of course, if we go and call Modify Elements
and we hand it numbers as an argument,
now numbers hopefully is modified
and we can confirm that
with the evaluation of numbers once again.
And you can see that indeed,
each of the values was doubled
from its original value in memory.
So clearly, we define numbers outside the body
of the function Modify Elements
when we passed it into Modify Elements.
We saw that the result was
to modify the original list.
Now there's nothing in this original function definition
that says Items must be a list.
If we study the code,
we can see that whatever we're working on
has to be something that's modifiable,
and, of course, the only thing we know
with this syntax for accessing its elements
right now that's modifiable is a list element
so we know Items must be a list.
But I can pass whatever I want into Modify Elements.
If Python doesn't like what I pass it,
it will tell me by issuing an exception at runtime.
So let's go and create another collection.
Let's create a Numbers Tuple.
And let's assign it a tuple
of 10, 20 and 30.
And, of course, we can evaluate that to see its contents,
and now let's try to call Modify Elements again.
This time we'll call it with Numbers Tuple.
Now when I do that, you can see here
that I'm getting an exception at execution time.
There's two pieces to the exception message
indicating line numbers where problems occurred,
so I do want to talk about that a little bit.
And we can see at the bottom of the error message,
a tuple object does not support item assignment.
So we're getting a type error
because tuple's can't do what we're trying to do
in the body of our Modify Elements function.
Now if you look at the trace back,
it tells you information about what happened
in this particular case.
So first of all,
it's telling me
that I called Modify Elements in snippet number eight,
which if I look up above is right here
in snippet number eight and that caused a problem.
And then separately it's saying in sippet number one,
which if I scroll back up
is the definition of Modify Elements
in snippet number one.
In line four of that snippet is where that problem occurred
and the problem at that point
was the fact that tuples don't support Item assignment.
Remember tuples are immutable, nonmodifiable.
So what you're seeing here are actual line numbers
in snippet number one.
So let's go look back at line four of snippet one.
So one, two, three, four.
This is line number four of Modify Elements,
and that's where the problem occurred.
Now all of that information
in the trace back can be used
to help you debug your code
when you run into these types of problems at execution time.