1 00:00:00,088 --> 00:00:01,550 - [Narrator] In this video 2 00:00:01,550 --> 00:00:02,760 I'd like to talk more 3 00:00:02,760 --> 00:00:05,216 about passing objects to functions. 4 00:00:05,216 --> 00:00:08,744 Now you may recall back in the preceding lesson 5 00:00:08,744 --> 00:00:10,900 that we talked about the fact 6 00:00:10,900 --> 00:00:14,040 that every single object in Python 7 00:00:14,040 --> 00:00:17,130 is passed to a function by reference. 8 00:00:17,130 --> 00:00:18,758 And the reason for that is, 9 00:00:18,758 --> 00:00:21,250 in Python everything is an object, 10 00:00:21,250 --> 00:00:25,310 and the way you manipulate objects is through references 11 00:00:25,310 --> 00:00:26,610 to those objects, 12 00:00:26,610 --> 00:00:29,100 not by having direct access 13 00:00:29,100 --> 00:00:30,870 to the objects themselves. 14 00:00:30,870 --> 00:00:34,830 So when I pass something like a list to a function, 15 00:00:34,830 --> 00:00:36,860 because a list is modifiable 16 00:00:36,860 --> 00:00:38,669 and because the function is receiving 17 00:00:38,669 --> 00:00:42,386 what is in fact the location of that list in memory, 18 00:00:42,386 --> 00:00:44,470 the function can talk directly 19 00:00:44,470 --> 00:00:46,010 to the list object 20 00:00:46,010 --> 00:00:47,500 through that reference 21 00:00:47,500 --> 00:00:49,898 and modify the contents of the list. 22 00:00:49,898 --> 00:00:51,800 So I'm going to paste a snippet 23 00:00:51,800 --> 00:00:53,920 into this session here 24 00:00:53,920 --> 00:00:56,240 for a function called Modify Elements 25 00:00:56,240 --> 00:00:57,490 which is going to multiply 26 00:00:57,490 --> 00:01:00,200 all of the elements in Items, 27 00:01:00,200 --> 00:01:01,612 its parameter by two, 28 00:01:01,612 --> 00:01:03,420 and we want to use this 29 00:01:03,420 --> 00:01:04,890 to demonstrate the fact that 30 00:01:04,890 --> 00:01:07,270 when pass a list into a function, 31 00:01:07,270 --> 00:01:09,220 the function can modify the list. 32 00:01:09,220 --> 00:01:10,350 So we're going to iterate 33 00:01:10,350 --> 00:01:12,890 through every index representing 34 00:01:12,890 --> 00:01:14,100 the element positions 35 00:01:14,100 --> 00:01:15,728 within the list called Items 36 00:01:15,728 --> 00:01:17,800 and for each of those items, 37 00:01:17,800 --> 00:01:21,050 we're going to multiple its value by two 38 00:01:21,050 --> 00:01:25,010 and store the result back into the element 39 00:01:25,010 --> 00:01:26,360 within the original list, 40 00:01:26,360 --> 00:01:29,510 wherever that thing happens to be in memory. 41 00:01:29,510 --> 00:01:31,670 So let's go ahead define that function 42 00:01:31,670 --> 00:01:34,770 and, of course, we need some values to work with 43 00:01:34,770 --> 00:01:36,780 so let's create a numbers list 44 00:01:36,780 --> 00:01:38,683 and we'll give it some values. 45 00:01:39,790 --> 00:01:41,960 Let's do like 5 values here 46 00:01:41,960 --> 00:01:44,270 and let's evaluate numbers 47 00:01:44,270 --> 00:01:46,430 just so we can see it's contents. 48 00:01:46,430 --> 00:01:50,300 And, of course, if we go and call Modify Elements 49 00:01:50,300 --> 00:01:53,610 and we hand it numbers as an argument, 50 00:01:53,610 --> 00:01:56,820 now numbers hopefully is modified 51 00:01:56,820 --> 00:01:58,820 and we can confirm that 52 00:01:58,820 --> 00:02:02,270 with the evaluation of numbers once again. 53 00:02:02,270 --> 00:02:04,010 And you can see that indeed, 54 00:02:04,010 --> 00:02:05,677 each of the values was doubled 55 00:02:05,677 --> 00:02:08,260 from its original value in memory. 56 00:02:08,260 --> 00:02:12,360 So clearly, we define numbers outside the body 57 00:02:12,360 --> 00:02:14,440 of the function Modify Elements 58 00:02:14,440 --> 00:02:17,210 when we passed it into Modify Elements. 59 00:02:17,210 --> 00:02:19,280 We saw that the result was 60 00:02:19,280 --> 00:02:21,470 to modify the original list. 61 00:02:21,470 --> 00:02:24,864 Now there's nothing in this original function definition 62 00:02:24,864 --> 00:02:28,700 that says Items must be a list. 63 00:02:28,700 --> 00:02:30,089 If we study the code, 64 00:02:30,089 --> 00:02:33,168 we can see that whatever we're working on 65 00:02:33,168 --> 00:02:35,820 has to be something that's modifiable, 66 00:02:35,820 --> 00:02:37,400 and, of course, the only thing we know 67 00:02:37,400 --> 00:02:39,950 with this syntax for accessing its elements 68 00:02:39,950 --> 00:02:44,049 right now that's modifiable is a list element 69 00:02:44,049 --> 00:02:46,372 so we know Items must be a list. 70 00:02:46,372 --> 00:02:51,298 But I can pass whatever I want into Modify Elements. 71 00:02:51,298 --> 00:02:54,520 If Python doesn't like what I pass it, 72 00:02:54,520 --> 00:02:58,380 it will tell me by issuing an exception at runtime. 73 00:02:58,380 --> 00:03:01,690 So let's go and create another collection. 74 00:03:01,690 --> 00:03:03,593 Let's create a Numbers Tuple. 75 00:03:04,440 --> 00:03:06,640 And let's assign it a tuple 76 00:03:06,640 --> 00:03:09,530 of 10, 20 and 30. 77 00:03:09,530 --> 00:03:14,530 And, of course, we can evaluate that to see its contents, 78 00:03:14,840 --> 00:03:18,090 and now let's try to call Modify Elements again. 79 00:03:18,090 --> 00:03:20,613 This time we'll call it with Numbers Tuple. 80 00:03:21,630 --> 00:03:24,110 Now when I do that, you can see here 81 00:03:24,110 --> 00:03:28,620 that I'm getting an exception at execution time. 82 00:03:28,620 --> 00:03:31,142 There's two pieces to the exception message 83 00:03:31,142 --> 00:03:34,460 indicating line numbers where problems occurred, 84 00:03:34,460 --> 00:03:36,620 so I do want to talk about that a little bit. 85 00:03:36,620 --> 00:03:38,529 And we can see at the bottom of the error message, 86 00:03:38,529 --> 00:03:42,850 a tuple object does not support item assignment. 87 00:03:42,850 --> 00:03:44,390 So we're getting a type error 88 00:03:44,390 --> 00:03:47,930 because tuple's can't do what we're trying to do 89 00:03:47,930 --> 00:03:51,650 in the body of our Modify Elements function. 90 00:03:51,650 --> 00:03:53,470 Now if you look at the trace back, 91 00:03:53,470 --> 00:03:56,750 it tells you information about what happened 92 00:03:56,750 --> 00:03:58,480 in this particular case. 93 00:03:58,480 --> 00:03:59,820 So first of all, 94 00:03:59,820 --> 00:04:00,810 it's telling me 95 00:04:00,810 --> 00:04:04,938 that I called Modify Elements in snippet number eight, 96 00:04:04,938 --> 00:04:07,740 which if I look up above is right here 97 00:04:07,740 --> 00:04:10,741 in snippet number eight and that caused a problem. 98 00:04:10,741 --> 00:04:15,741 And then separately it's saying in sippet number one, 99 00:04:16,190 --> 00:04:17,570 which if I scroll back up 100 00:04:17,570 --> 00:04:20,037 is the definition of Modify Elements 101 00:04:20,037 --> 00:04:21,630 in snippet number one. 102 00:04:21,630 --> 00:04:25,658 In line four of that snippet is where that problem occurred 103 00:04:25,658 --> 00:04:27,674 and the problem at that point 104 00:04:27,674 --> 00:04:32,050 was the fact that tuples don't support Item assignment. 105 00:04:32,050 --> 00:04:36,410 Remember tuples are immutable, nonmodifiable. 106 00:04:36,410 --> 00:04:39,671 So what you're seeing here are actual line numbers 107 00:04:39,671 --> 00:04:41,560 in snippet number one. 108 00:04:41,560 --> 00:04:44,320 So let's go look back at line four of snippet one. 109 00:04:44,320 --> 00:04:46,540 So one, two, three, four. 110 00:04:46,540 --> 00:04:49,490 This is line number four of Modify Elements, 111 00:04:49,490 --> 00:04:52,270 and that's where the problem occurred. 112 00:04:52,270 --> 00:04:53,950 Now all of that information 113 00:04:53,950 --> 00:04:55,500 in the trace back can be used 114 00:04:55,500 --> 00:04:57,410 to help you debug your code 115 00:04:57,410 --> 00:05:01,143 when you run into these types of problems at execution time.