1 00:00:00,000 --> 00:00:02,640 Hello, again. And in this lecture, I 2 00:00:02,640 --> 00:00:04,770 would like to make sure you know the 3 00:00:04,770 --> 00:00:06,630 terminology of Object Oriented 4 00:00:06,630 --> 00:00:10,290 Programming in Python. So now, we have the 5 00:00:10,290 --> 00:00:14,040 class, which is this blueprint here. Or 6 00:00:14,040 --> 00:00:17,940 you can call it a prototype that defines 7 00:00:17,970 --> 00:00:21,120 the characteristics of an object that is 8 00:00:21,120 --> 00:00:23,760 about to be created. So that's the class 9 00:00:23,790 --> 00:00:26,610 and then we have the object instance, or 10 00:00:26,610 --> 00:00:29,670 simply the object. So this here is an 11 00:00:29,670 --> 00:00:33,000 object, so the checking account object 12 00:00:33,030 --> 00:00:37,350 that stores data in this specific file 13 00:00:37,350 --> 00:00:40,680 path. And it has these attributes. So it 14 00:00:40,680 --> 00:00:43,860 has this fee, then we have instance 15 00:00:43,860 --> 00:00:46,980 variables. And these are variables that 16 00:00:46,980 --> 00:00:50,280 are defined inside the methods or the 17 00:00:50,280 --> 00:00:54,330 class like filepath here. And these are 18 00:00:54,330 --> 00:00:56,640 accessible by the instance by the 19 00:00:56,640 --> 00:00:58,800 object instance, that is created by that 20 00:00:58,800 --> 00:01:01,830 class. And then we also have something 21 00:01:01,830 --> 00:01:04,590 called class variable. So instance 22 00:01:04,590 --> 00:01:07,590 variable and class variables. Now, we 23 00:01:07,590 --> 00:01:10,710 didn't create a class variable in any of 24 00:01:10,710 --> 00:01:12,900 the previous lectures. So I'll go ahead 25 00:01:12,900 --> 00:01:15,330 and create one now. Let's create one 26 00:01:15,330 --> 00:01:17,670 for our checking account. So that would 27 00:01:17,670 --> 00:01:24,616 be something like, let's say type equals to checking. 28 00:01:24,617 --> 00:01:27,156 [No audio] 29 00:01:27,158 --> 00:01:30,461 So our class variable is declared outside of the 30 00:01:30,462 --> 00:01:35,340 methods of that class. And class variables are 31 00:01:35,340 --> 00:01:38,430 shared by all the instances of a class. 32 00:01:38,700 --> 00:01:41,490 So instance variables are shared by only the 33 00:01:41,490 --> 00:01:43,500 object instance. For instance, let me 34 00:01:43,500 --> 00:01:47,370 explain that. Here, these are one of our 35 00:01:47,370 --> 00:01:49,440 object instances. So we only have one 36 00:01:49,440 --> 00:01:52,359 object instance here, but we could have more. 37 00:01:53,191 --> 00:01:56,093 So I'll copy this and create another object 38 00:01:56,094 --> 00:02:00,289 instance here. And I'll call this, let's say, this is 39 00:02:00,813 --> 00:02:06,869 jacks_checking account. So I'll replace that, 40 00:02:08,638 --> 00:02:15,120 and that, and that. This is johns_checking account. 41 00:02:15,121 --> 00:02:22,588 [Author typing] 42 00:02:22,590 --> 00:02:23,961 And they also have there 43 00:02:23,962 --> 00:02:26,321 [No audio] 44 00:02:26,323 --> 00:02:29,648 personal storage there. So let's say jack.txt, 45 00:02:31,062 --> 00:02:37,952 and john.txt. So Jack has 46 00:02:38,364 --> 00:02:42,211 $100 and John has $200. 47 00:02:43,448 --> 00:02:45,409 Good. So we need to change 48 00:02:45,419 --> 00:02:51,993 that jacks, jack, and john. 49 00:02:51,994 --> 00:02:56,540 [Author typing] 50 00:02:56,542 --> 00:02:59,550 And if we execute this script now 51 00:02:59,551 --> 00:03:04,798 [Author typing] 52 00:03:04,800 --> 00:03:06,810 we will get two values printed out. So 53 00:03:06,990 --> 00:03:09,930 minus 1 for the statement here, 54 00:03:10,410 --> 00:03:15,480 because the Jack had, so he had $100 55 00:03:15,480 --> 00:03:17,760 there, but when he transferred 100, and 56 00:03:17,760 --> 00:03:21,780 then minus the fee of $1. And then 57 00:03:21,780 --> 00:03:27,330 he ended up with minus $1 there. He 58 00:03:27,330 --> 00:03:30,750 owes the bank some money. And then John 59 00:03:30,750 --> 00:03:34,530 had 200. So he has a 99 now. So what 60 00:03:34,530 --> 00:03:37,260 I'm trying to say is that these 61 00:03:37,260 --> 00:03:40,500 variables belong only to their object. 62 00:03:41,220 --> 00:03:48,153 But if you try now, jacks_checking.type, 63 00:03:48,680 --> 00:03:50,179 so you want to print that 64 00:03:50,180 --> 00:03:54,890 [Author typing] 65 00:03:54,891 --> 00:04:00,119 print johns_checking.type. 66 00:04:00,120 --> 00:04:03,336 [Author typing] 67 00:04:03,337 --> 00:04:06,780 You'll see that this class variable is shared by 68 00:04:06,810 --> 00:04:09,990 all the instances of that class. So 69 00:04:09,990 --> 00:04:12,570 that's the difference between class and 70 00:04:12,570 --> 00:04:14,820 instance variables. Class variables are 71 00:04:14,850 --> 00:04:18,780 rarely used to you'll rarely have to 72 00:04:18,810 --> 00:04:21,870 pass them in here. Another concept now. 73 00:04:22,800 --> 00:04:26,850 Docstrings. And a docstring is usually 74 00:04:26,880 --> 00:04:30,900 passed as soon as you arrive the class 75 00:04:30,900 --> 00:04:32,910 keywords are just down the class keyword 76 00:04:33,720 --> 00:04:35,670 that provides the explanation of your 77 00:04:35,670 --> 00:04:39,720 class. Let's say this class generates 78 00:04:40,710 --> 00:04:45,960 checking account objects, then you 79 00:04:46,410 --> 00:04:48,090 close that with three quotes. 80 00:04:48,091 --> 00:04:50,974 [No audio] 81 00:04:50,976 --> 00:04:56,100 So what that does is, you know if we print that 82 00:04:56,130 --> 00:05:01,541 out, so we grab one of our object instances, let's say, 83 00:05:01,906 --> 00:05:08,100 johns_checking.__doc and 84 00:05:08,102 --> 00:05:12,212 double underscore again. So print that. 85 00:05:12,213 --> 00:05:19,978 [Author typing] 86 00:05:19,980 --> 00:05:22,470 So as you see, a docstring is used to 87 00:05:22,470 --> 00:05:24,540 provide some information about that 88 00:05:24,540 --> 00:05:28,230 class. So it's good to define the string 89 00:05:28,230 --> 00:05:29,850 when you're writing classes so that 90 00:05:29,850 --> 00:05:33,000 others, if they use your class, and they 91 00:05:33,000 --> 00:05:35,730 can apply this operation here, they can 92 00:05:35,730 --> 00:05:38,880 see what they are classes about. This is 93 00:05:38,880 --> 00:05:41,310 particularly useful when you import in 94 00:05:41,310 --> 00:05:44,100 classes from modules. So when you don't 95 00:05:44,100 --> 00:05:48,330 actually see the code of your class, and 96 00:05:48,330 --> 00:05:51,240 you can do these and you can, you have 97 00:05:51,240 --> 00:05:53,220 access to the docstring of the class. 98 00:05:53,760 --> 00:05:56,190 And then we have a concept of data 99 00:05:56,190 --> 00:05:59,820 members, which actually is referred to 100 00:06:00,000 --> 00:06:02,730 class variables or instance variables. 101 00:06:03,960 --> 00:06:06,810 So this is a data member, and this also 102 00:06:06,810 --> 00:06:10,110 is a data member as well. Then we have 103 00:06:10,110 --> 00:06:12,840 the constructor, which was this init 104 00:06:12,840 --> 00:06:15,646 function. And as you know the init 105 00:06:15,648 --> 00:06:17,280 function so the constructor 106 00:06:17,280 --> 00:06:20,460 constructs your class. And then you have 107 00:06:20,460 --> 00:06:23,610 class methods that you apply to your 108 00:06:23,610 --> 00:06:26,640 object instance. So transfer is a class 109 00:06:26,640 --> 00:06:29,850 method. So this is also a method but 110 00:06:29,850 --> 00:06:31,530 it's a spatial method. So it's a 111 00:06:31,530 --> 00:06:34,740 constructor. And instantiation. If you 112 00:06:34,740 --> 00:06:36,360 hear about instantiation, there's a 113 00:06:36,360 --> 00:06:39,960 process of creating object instances. So 114 00:06:39,960 --> 00:06:43,897 instances of a class like this is instantiation 115 00:06:43,898 --> 00:06:46,652 of the class, and this one here as well. 116 00:06:46,653 --> 00:06:48,927 [No audio] 117 00:06:48,929 --> 00:06:51,000 You know, inheritance. Now 118 00:06:51,030 --> 00:06:52,890 inheritance is when you create a 119 00:06:52,890 --> 00:06:57,720 subclass out of a base class. And this 120 00:06:57,720 --> 00:07:00,480 subclass shares the methods or the base 121 00:07:00,480 --> 00:07:04,140 class. Plus it has its own methods that 122 00:07:04,140 --> 00:07:08,880 are specific to that subclass. Sometimes 123 00:07:08,880 --> 00:07:11,670 you also hear about attributes and 124 00:07:11,670 --> 00:07:14,520 attributes are these one here, so when 125 00:07:14,520 --> 00:07:18,450 you access these instance variables, or 126 00:07:18,480 --> 00:07:21,150 even so class variables, or even 127 00:07:21,150 --> 00:07:25,020 instance variables. I hope you found 128 00:07:25,020 --> 00:07:27,810 this useful. And I'll talk to you either 129 00:07:27,810 --> 00:07:31,140 next lecture. So we're done with this 130 00:07:31,140 --> 00:07:33,141 section, and yeah, see you later.