1 00:00:07,850 --> 00:00:11,260 Welcome back. So, last time we talked 2 00:00:11,260 --> 00:00:13,780 about creating classes and again the way that you create 3 00:00:13,780 --> 00:00:16,300 a class is you say class and then 4 00:00:16,300 --> 00:00:19,555 the name of the class which usually starts with a capital letter. 5 00:00:19,555 --> 00:00:21,895 So, I'll say class point. 6 00:00:21,895 --> 00:00:25,690 I'm going to leave opening and closing parentheses here. 7 00:00:25,690 --> 00:00:32,865 Then you usually declare constructor method which is underscore init, underscore. 8 00:00:32,865 --> 00:00:35,895 Every method takes in an argument self. 9 00:00:35,895 --> 00:00:37,815 In the case of point, 10 00:00:37,815 --> 00:00:41,370 we'll call x and y, 11 00:00:41,370 --> 00:00:43,865 the arguments of the constructor and 12 00:00:43,865 --> 00:00:46,570 then we set the instance variables inside of the constructor. 13 00:00:46,570 --> 00:00:53,590 So, I'll say self dot x and self dot y equals y. 14 00:00:54,800 --> 00:00:58,165 In this lecture, I want to give some more detail 15 00:00:58,165 --> 00:01:01,130 about what actually happens when we construct 16 00:01:01,130 --> 00:01:03,700 in instance of this point class and how 17 00:01:03,700 --> 00:01:07,345 Python actually searches for instance variables and methods. 18 00:01:07,345 --> 00:01:09,520 So, we construct a new point, 19 00:01:09,520 --> 00:01:18,580 I'll say one equals point and then we need to pass in values for x and y. 20 00:01:18,580 --> 00:01:26,300 So, I'll say that x is equal to 10 and y is equal to 100 for point one. 21 00:01:29,600 --> 00:01:32,570 So, let's suppose that we declared two methods. 22 00:01:32,570 --> 00:01:38,015 Get x which returns 23 00:01:38,015 --> 00:01:42,650 self dot x and get 24 00:01:42,650 --> 00:01:49,445 y which returns self dot y. 25 00:01:49,445 --> 00:01:51,740 When we declare these methods, 26 00:01:51,740 --> 00:01:56,630 what Python does is it has some internal representation of the class itself. 27 00:01:56,630 --> 00:02:00,350 Now, we've talked about the metaphor of classes as factories. 28 00:02:00,350 --> 00:02:06,375 So, I'm going to visually represent a class as this factory like object. 29 00:02:06,375 --> 00:02:11,100 So, you can imagine needs smokestacks and this is the factory. 30 00:02:11,100 --> 00:02:14,305 So, when we have an init method, 31 00:02:14,305 --> 00:02:20,950 we're saying that we have one method inside of the class called underscore init. 32 00:02:22,910 --> 00:02:28,085 This is a function like object that lives within the class. 33 00:02:28,085 --> 00:02:37,650 So, it accepts three arguments it accepts self and it accepts arguments for x and y. 34 00:02:38,150 --> 00:02:42,110 So, this method in underscore in it, 35 00:02:42,110 --> 00:02:45,605 the constructor lives inside of the class. 36 00:02:45,605 --> 00:02:47,960 Then when we have get x, 37 00:02:47,960 --> 00:02:50,290 this method also lives inside of the class. 38 00:02:50,290 --> 00:02:55,350 So, we have this method inside of the class get x. 39 00:02:55,690 --> 00:03:05,610 In this case it takes in one argument which is self and then we have the method get y. 40 00:03:07,340 --> 00:03:11,935 Again get why lives inside of this class. 41 00:03:11,935 --> 00:03:17,270 Now, on line 10 when we create a new instance of point what this is doing, 42 00:03:17,270 --> 00:03:20,600 is it's asking the class to create a new instance. 43 00:03:20,600 --> 00:03:25,590 I'm going to represent this instance like so. 44 00:03:26,710 --> 00:03:30,530 In every instance has a set of instance variables, 45 00:03:30,530 --> 00:03:34,400 in this case the instance variables are x, 46 00:03:34,400 --> 00:03:38,390 which we set to 10 and y, 47 00:03:38,390 --> 00:03:41,365 which we set to 100. 48 00:03:41,365 --> 00:03:45,320 Now, I want to go into a little more detail as to how exactly we 49 00:03:45,320 --> 00:03:49,205 set these instance variables x and y to 10 and100. 50 00:03:49,205 --> 00:03:54,775 In other words, what exactly happens when we say point 10,100? 51 00:03:54,775 --> 00:03:58,130 So, I'm going to erase this instance for now. 52 00:03:58,130 --> 00:04:00,170 Let's start from scratch. 53 00:04:00,170 --> 00:04:04,325 So, when Python evaluates this statement point 10,100, 54 00:04:04,325 --> 00:04:10,025 then what it does is it first creates a new kind of blank slate instance. 55 00:04:10,025 --> 00:04:12,140 So, it creates a new instance of 56 00:04:12,140 --> 00:04:18,970 point and it doesn't have any instance variables at first. 57 00:04:18,970 --> 00:04:22,030 The next thing that Python does is it asks, 58 00:04:22,030 --> 00:04:27,190 does this point class have a constructor or in other words this underscore init method? 59 00:04:27,190 --> 00:04:30,400 In the case of point there is an underscore init method. 60 00:04:30,400 --> 00:04:37,780 So, what Python does is it calls that underscore init method on this empty instance. 61 00:04:37,780 --> 00:04:42,640 So, what it's going to do is it's going to take this instance and pass it 62 00:04:42,640 --> 00:04:48,030 in as the value for self when we call the constructor. 63 00:04:48,030 --> 00:04:53,320 In all of this is done before we even have finished evaluating this statement. 64 00:04:53,320 --> 00:04:57,370 So, we take this instance in and that's the value of 65 00:04:57,370 --> 00:05:01,160 self and then in this case we're also passing in two additional arguments. 66 00:05:01,160 --> 00:05:08,610 We're passing in 10 for x and 100 for y. 67 00:05:09,950 --> 00:05:14,780 When we're passing in values for x and y these are just function arguments. 68 00:05:14,780 --> 00:05:18,380 So, in other words even though x just so happens 69 00:05:18,380 --> 00:05:22,355 to be the name of what's going to later on be an instance variable. 70 00:05:22,355 --> 00:05:25,340 There's nothing special about the value x. 71 00:05:25,340 --> 00:05:30,140 So, now we're calling the constructor underscore init and we're 72 00:05:30,140 --> 00:05:35,795 passing in our new instance which is blank and we're passing in again values for x and y. 73 00:05:35,795 --> 00:05:40,125 Now, on line three when we say self dot x equals x. 74 00:05:40,125 --> 00:05:44,750 Then what that does is it sets the instance variable for this instance. 75 00:05:44,750 --> 00:05:53,110 So, it's setting the instance variable x to whatever this value x that we passed in it. 76 00:05:53,110 --> 00:05:56,745 So, in this case we passed in 10 for x. 77 00:05:56,745 --> 00:06:03,395 So, we're setting the instance variable for this instance x to 10. Same thing for y. 78 00:06:03,395 --> 00:06:08,880 In this case we're setting y to the value 100 because that was what was passed in. 79 00:06:10,600 --> 00:06:16,955 So, now we have this instance in it has two instance variables x and y. 80 00:06:16,955 --> 00:06:20,430 That's all for now, until next time.