All language subtitles for 2. Instances, Constructors, Self and more

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese Download
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 1 00:00:05,660 --> 00:00:10,950 so continuing on from our previous video so we ended up talking about variables 2 2 00:00:10,950 --> 00:00:15,320 but saying that when a variable is bound to an instance of a class in 3 3 00:00:15,320 --> 00:00:20,130 Python its referred to as a data attribute now their other object orientated 4 4 00:00:20,130 --> 00:00:24,770 terms for the same thing so you might find variable attributes also called 5 5 00:00:24,770 --> 00:00:30,810 fields in languages such as Java or data members in C++ and I'll introduced yet 6 6 00:00:30,810 --> 00:00:32,060 another term shortly 7 7 00:00:32,060 --> 00:00:35,950 one borrowed from the small talk language but I over simplified things 8 8 00:00:35,950 --> 00:00:40,840 here and now we will be talking about different kinds of methods and 9 9 00:00:40,840 --> 00:00:46,270 attributes later but that's the basic jargon relating to classes which you 10 10 00:00:46,270 --> 00:00:50,280 should probably become familiar with now before I summarise these terms 11 11 00:00:50,280 --> 00:00:54,020 there's another way to use a string replacement fields that is useful when 12 12 00:00:54,020 --> 00:00:59,690 dealing with classes so because Kenwood and Hamilton are objects we can specify 13 13 00:00:59,690 --> 00:01:03,960 their attributes in the replacement fields and let's have a look at that 14 14 00:01:03,960 --> 00:01:08,550 to see how we can do that so we are gonna add that on line 20 we type.... 15 15 00:01:14,710 --> 00:01:28,860 .... 16 16 00:01:30,670 --> 00:01:44,070 ...so here because Kenwood and 17 17 00:01:44,070 --> 00:01:47,960 Hamilton are objects we can specify their attributes in the replacement 18 18 00:01:47,960 --> 00:01:52,290 fields so if we run this just to confirm we should see the identical results the 19 19 00:01:52,290 --> 00:02:01,480 print out as you can see their exactly the same but looking at the code I will argue that line 20 is easy to read because 20 20 00:02:01,480 --> 00:02:05,790 you can see immediately which attributes are being used at each point in the 21 21 00:02:05,790 --> 00:02:10,200 string and of course on line 18 you couldn't see that because there wasn't anything in the 22 22 00:02:10,200 --> 00:02:17,680 braces so just to summarize where we are at just gonna paste in that code in their now paste in those 23 23 00:02:17,680 --> 00:02:23,530 comments to be more precise so a class is a template for creating objects all 24 24 00:02:23,530 --> 00:02:26,950 objects created using the same class will have the same characteristics 25 25 00:02:26,950 --> 00:02:34,360 object is an instance of a class instantiate we didn't used the word instantiate but 26 26 00:02:34,360 --> 00:02:39,710 that is how the process of creating an instance of a class method is a function 27 27 00:02:39,710 --> 00:02:44,960 defined in a class and attribute is a variable bound to an instance of a 28 28 00:02:44,960 --> 00:02:50,390 class so as you can see here a class is a template for creating objects the 29 29 00:02:50,390 --> 00:02:53,810 template defines the attributes and methods that all instances of the class 30 30 00:02:53,810 --> 00:02:56,580 will have now although this is not true 31 31 00:02:56,580 --> 00:03:00,950 of other object oriented programming languages in Python a class is also 32 32 00:03:00,950 --> 00:03:06,070 exactly the same thing as a type other languages might refer to Kingwood in 33 33 00:03:06,070 --> 00:03:11,150 our sample program is being of type kettle but they also have 34 34 00:03:11,150 --> 00:03:17,250 types that are not classes but in Python every type is a class which is why we 35 35 00:03:17,250 --> 00:03:21,310 were able to call the add method of our int object at the start of the previous 36 36 00:03:21,310 --> 00:03:22,230 video 37 37 00:03:22,230 --> 00:03:26,530 an object is another name for an instance of a class then once the class 38 38 00:03:26,530 --> 00:03:30,980 has been defined it is the actual instances that are used in a program to 39 39 00:03:30,980 --> 00:03:34,640 store the values of the attributes and perform actions on them and you saw 40 40 00:03:34,640 --> 00:03:39,280 instances being created on line 9 and line 16 on the screen so when 41 41 00:03:39,280 --> 00:03:43,400 creating a new object from a class definition the terms uses instantiating 42 42 00:03:43,400 --> 00:03:48,210 an object and that's just a fancy word for creating an instance of a class but 43 43 00:03:48,210 --> 00:03:53,050 you should be familiar with that term now method is a function as bound to an 44 44 00:03:53,050 --> 00:03:57,010 instance of a class now in the Tkinter videos you might recall we created 45 45 00:03:57,010 --> 00:04:01,430 instances of the back label and canvas classes and we used their grid and pack 46 46 00:04:01,430 --> 00:04:07,000 methods to place them on the screen and finally an attribute is a variable that 47 47 00:04:07,000 --> 00:04:11,570 is bound to an instance of a class so the kettle variable and kenwood can keep 48 48 00:04:11,570 --> 00:04:15,990 track of the values of its making price without affecting the attributes 49 49 00:04:15,990 --> 00:04:22,180 of the Hamilton object which has got its own values for those attributes so as I 50 50 00:04:22,180 --> 00:04:26,060 hinted at earlier this is a slight over-simplification in the last two 51 51 00:04:26,060 --> 00:04:29,960 definitions and i will be explaining a bit more about that a little bit later on in the 52 52 00:04:29,960 --> 00:04:33,920 section but we have to walk here before we can run so those definitions will give 53 53 00:04:33,920 --> 00:04:37,910 us something to work with while we get the hang of classes and object oriented 54 54 00:04:37,910 --> 00:04:42,940 programming in general so continuing on now with our kettle class at the moment 55 55 00:04:42,940 --> 00:04:47,880 it has 2 data attributes make and price and a single method called init 56 56 00:04:47,880 --> 00:04:51,060 __init__ which you can see is 57 57 00:04:51,060 --> 00:04:55,790 showing on line three and if you look at that lit looks like it's sets the values for 58 58 00:04:55,790 --> 00:04:58,790 the attributes but also looks a little strange 59 59 00:04:59,360 --> 00:05:04,110 now as I explain what's going on here I'm gonna be referring to quite a few things whos names 60 60 00:05:04,110 --> 00:05:08,920 start with and end with 2 underscores and it's going to get a lil bit tedious 61 61 00:05:08,920 --> 00:05:12,410 if I keep saying __ all the time so I'm just going to use the name 62 62 00:05:12,410 --> 00:05:16,070 of the object and you can see on the screen that its got underscores around 63 63 00:05:16,070 --> 00:05:19,750 it but I will review this use of underscores and how it relates to 64 64 00:05:19,750 --> 00:05:23,420 classes later on but just keep in mind that time when we refer to something for 65 65 00:05:23,420 --> 00:05:26,810 example on line 3 as init I'm referring to __init__ 66 66 00:05:26,810 --> 00:05:32,010 now we looked at most of the users of underscore around 67 67 00:05:32,010 --> 00:05:34,000 variable names in the previous section 68 68 00:05:34,000 --> 00:05:38,910 and theirs a little bit more to add to that once we get more into classes now this 69 69 00:05:38,910 --> 00:05:39,640 class 70 70 00:05:39,640 --> 00:05:44,310 obviously doesn't do a great deal and theirs not really a lot going on and it's 71 71 00:05:44,310 --> 00:05:47,700 not really a lot you can do with a kettle other than switching it on and wait but 72 72 00:05:47,700 --> 00:05:52,910 I will add a method to do that before looking at more complicated classes now the Python 73 73 00:05:52,910 --> 00:05:55,200 style guide pep8 by the way 74 74 00:05:55,200 --> 00:05:59,290 recommends just single blank line between methods inside a class which is 75 75 00:05:59,290 --> 00:06:03,810 different to the recommendation and there should be two blank lines between 76 76 00:06:03,810 --> 00:06:09,030 top level functions in a module so if you use 2 blanks lines then IntelliJ will show 77 77 00:06:09,030 --> 00:06:12,750 warning and personally I like to leave 2 blank lines after class definitions 78 78 00:06:12,750 --> 00:06:17,290 but this is not covered in the style guide and you won't get a warning unless you leave more than two blank 79 79 00:06:17,290 --> 00:06:18,630 lines and 80 80 00:06:18,630 --> 00:06:21,630 I'm gonna add a second method now 81 81 00:06:23,280 --> 00:06:34,570 and if we start this....and notice as I press the left bracket 82 82 00:06:34,570 --> 00:06:39,790 IntelliJ automatically added the self parameter so as I mentioned functions that are bound to a 83 83 00:06:39,790 --> 00:06:44,530 class called methods and main difference between a method and a function is the 84 84 00:06:44,530 --> 00:06:49,390 presence of this self parameter that's been added automatically just go ahead and 85 85 00:06:49,390 --> 00:07:01,660 finish this method....and just a reminder if I go to and add that as a 86 86 00:07:01,660 --> 00:07:06,570 second add 2 lines between methods we get the warning from IntelliJ 2 87 87 00:07:06,570 --> 00:07:07,780 blanks lines so if your in a 88 88 00:07:07,780 --> 00:07:14,330 class only have one line between methods to remove that warning and you might be 89 89 00:07:14,330 --> 00:07:18,200 wondering what is the self parameter well so there's nothing really special 90 90 00:07:18,200 --> 00:07:23,200 about the name self or the parameter and you have called it anything you wanted avoiding 91 91 00:07:23,200 --> 00:07:28,140 name conflicts with other objects of course but unlike to key word this in Java for 92 92 00:07:28,140 --> 00:07:32,910 example self is just the name of a parameter the important thing is the 93 93 00:07:32,910 --> 00:07:36,910 presence of the parameter not what its called so having said that the 94 94 00:07:36,910 --> 00:07:40,950 convention is that it's called self and although this is not enforced in any way 95 95 00:07:40,950 --> 00:07:45,010 it's not a good idea to change the convention is so well established that 96 96 00:07:45,010 --> 00:07:47,160 tools may expect to be called self and may not 97 97 00:07:47,160 --> 00:07:52,420 work if you use another name in fact as we have just seen IntelliJ automatically use the 98 98 00:07:52,420 --> 00:07:53,830 name self for it 99 99 00:07:53,830 --> 00:07:57,820 when we are defining the method just now so what is self 100 100 00:07:57,820 --> 00:08:02,780 well the answer is that it's a reference to the instance of the class now if you 101 101 00:08:02,780 --> 00:08:07,670 used to C++ or Java then you can think of it as equivalent to this and it's 102 102 00:08:07,670 --> 00:08:11,730 used to refer to instance variables and you could see that we're doing that 103 103 00:08:11,730 --> 00:08:18,290 on lines four five and six and again on line 9 and although we've use many methods in the early 104 104 00:08:18,290 --> 00:08:21,910 lectures in this course we've never had to specify an argument for the self 105 105 00:08:21,910 --> 00:08:26,160 parameter and this is because Python supplies the value automatically when 106 106 00:08:26,160 --> 00:08:29,820 calling the method now I will talked about this a little bit more in a minute but first 107 107 00:08:29,820 --> 00:08:41,970 let's call the method and check to see if it is a kettle or not so we type.... 108 108 00:08:41,970 --> 00:08:53,830 ....and notice that again when I typed that.... 109 109 00:08:53,830 --> 00:08:59,340 ....you can see the definition shows the self parameter but when I press enter it automatically 110 110 00:08:59,340 --> 00:09:03,240 there's no parameter passed in the brackets and if we just confirm that.... 111 111 00:09:03,240 --> 00:09:15,020 ...and if we run this first time we ran it on line 33 was false 112 112 00:09:15,020 --> 00:09:19,930 that's because up here on line six we defined it as false second time after 113 113 00:09:19,930 --> 00:09:25,470 calling Hamilton.switch_on the variable the attribute in this case for 114 114 00:09:25,470 --> 00:09:31,470 our hamilton instance was set to true in other words the value of the hamiltons instance on 115 115 00:09:31,470 --> 00:09:35,190 started off as false because that's what it was initialized to in the constructor 116 116 00:09:35,190 --> 00:09:40,440 and calling the switch-on method on line 34 set to true which is the value of 117 117 00:09:40,440 --> 00:09:45,960 course we are getting when we print out the value for the second time on line 35 now the 118 118 00:09:45,960 --> 00:09:50,770 term constructor by the way refers to a special method that is executed when an 119 119 00:09:50,770 --> 00:09:55,590 instance of a class is created or constructed in Python this is the init 120 120 00:09:55,590 --> 00:09:58,180 method that you can see defined on line 3 121 121 00:09:58,180 --> 00:10:03,150 and as you saw when I typed in line 34 we don't provide a value for the 122 122 00:10:03,150 --> 00:10:07,000 self parameter when calling the switch_on method when you call a method 123 123 00:10:07,000 --> 00:10:08,300 on an instance 124 124 00:10:08,300 --> 00:10:12,560 Python automatically provides a reference to the instance as the first parameter to 125 125 00:10:12,560 --> 00:10:17,110 the method now we can see this in a bit more detail by not using and instance but 126 126 00:10:17,110 --> 00:10:21,860 calling the method the class itself instead so we come down here and put this 127 127 00:10:21,860 --> 00:10:30,780 on lines 37 we can type.... 128 128 00:10:30,780 --> 00:10:43,030 ...so if we run that you can see that we got the value true 129 129 00:10:43,030 --> 00:10:46,950 and of course Hamilton initially when we created the instance of the 130 130 00:10:46,950 --> 00:10:52,460 object was then constructor made it the value false that by calling this method in an 131 131 00:10:52,460 --> 00:10:57,080 alternative ways you can see they're using the class definition initially you 132 132 00:10:57,080 --> 00:11:00,960 can see what's happening there and the parameters has been passed so instead of 133 133 00:11:00,960 --> 00:11:05,420 using an instance of kettle to call a method we've use the class itself because 134 134 00:11:05,420 --> 00:11:09,830 there is no instance here the value for self has to be specified and failing to do so 135 135 00:11:09,830 --> 00:11:15,280 would give an error so the two forms have exactly the same effect and as you can see 136 136 00:11:15,280 --> 00:11:19,960 when we ran the program kettle switch on ended up returning true so we can see 137 137 00:11:19,960 --> 00:11:24,500 that it was updated correctly so lets go back to the definition again I just want to 138 138 00:11:24,500 --> 00:11:32,330 go back and delete this out...just to confirm that we may have miss this the first and when I add the bracket not the parameter 139 139 00:11:32,330 --> 00:11:36,110 showing up there as self so its asking for that parameter in this case and I can 140 140 00:11:36,110 --> 00:11:40,960 change it too kenwood but again just to confirm if we do the 141 141 00:11:40,960 --> 00:11:49,860 alternative syntax......so when I do that 142 142 00:11:49,860 --> 00:11:54,140 noting that the self parameter is showing at the top of the pop up but when 143 143 00:11:54,140 --> 00:11:59,260 I press enter notice how it didn't ask for the parameter in this case because it knows 144 144 00:11:59,260 --> 00:12:02,210 we've already got an instance that we're working on 145 145 00:12:02,210 --> 00:12:06,160 so the implementation of classes in Python can create and some unexpected 146 146 00:12:06,160 --> 00:12:07,550 behavior 147 147 00:12:07,550 --> 00:12:11,780 partly because it's a mix of modular three and C++ with some small talk thrown 148 148 00:12:11,780 --> 00:12:15,900 in for good measure and once again if you are learning object oriented programming for 149 149 00:12:15,900 --> 00:12:19,780 the first time using Python then you'll probably be quite happy with the way 150 150 00:12:19,780 --> 00:12:24,140 things work that's not to say you won't have to concentrate but there shouldn't be 151 151 00:12:24,140 --> 00:12:31,070 any surprises but for C++ and Java programmers it might be a good idea to put what you already know to the back 152 152 00:12:31,070 --> 00:12:35,310 of your mind and be prepared to learn a new way of thinking about things when it 153 153 00:12:35,310 --> 00:12:38,410 comes to classes and object-oriented programming in Python 154 154 00:12:39,410 --> 00:12:43,440 now just like everything else in Python classes are dynamic and it can be modified 155 155 00:12:43,440 --> 00:12:47,630 after their created now to see this in action and reinforce my previous comment 156 156 00:12:47,630 --> 00:12:51,690 about surprises for C++ and Java programmers I'm going to add a measure 157 157 00:12:51,690 --> 00:12:56,430 for the keyword kettles power and kilowatts let's go ahead and do that so 158 158 00:12:56,430 --> 00:13:03,730 come down here to line 41 a bit more space so we can see..... 159 159 00:13:03,730 --> 00:13:22,070 .... 160 160 00:13:27,970 --> 00:13:32,450 so this case now the Kenwood instance of the kettle class now has another 161 161 00:13:32,450 --> 00:13:38,470 data attribute called power with the value 1.5 now remember that variables 162 162 00:13:38,470 --> 00:13:42,310 in Python come into existence the first time they're assigned a value and the 163 163 00:13:42,310 --> 00:13:46,750 same thing is true for instance variables which is the small talk name for data 164 164 00:13:46,750 --> 00:13:52,140 attributes so here I created a data attribute for the Kenwood object called power 165 165 00:13:52,140 --> 00:13:58,940 and I gave it a value of 1.5 so the term instance variable is useful because it describes 166 166 00:13:58,940 --> 00:14:04,510 power as a variable that is bound to an instance of the kettle class so as a 167 167 00:14:04,510 --> 00:14:10,750 result the following code I'm gonna add shouldn't be surprising..... 168 168 00:14:10,750 --> 00:14:26,290 ...and if we run this we get an error message attribute kettle object has not attribute power so if I close that 169 169 00:14:26,290 --> 00:14:31,200 down again and that's because the Hamilton instance doesn't have a power attribute 170 170 00:14:31,200 --> 00:14:36,170 because if you're not created one by assigning a value to it on line 43 we added 171 171 00:14:36,170 --> 00:14:40,940 an instance variable for the Kenwood instance of the kettle class but of course we didn't 172 172 00:14:40,940 --> 00:14:44,710 do that for the Hamilton instance and that's why we got an error when we run 173 173 00:14:44,710 --> 00:14:45,510 the program 174 174 00:14:45,510 --> 00:14:50,280 alright so I'm going to finish this video here now in the next video we're going to talk more 175 175 00:14:50,280 --> 00:14:53,860 about objects and find a bit more about instance variables 176 176 00:14:54,360 --> 00:15:00,010 and continue on generally speaking in our discussion and in our journey finding out more 177 177 00:15:00,010 --> 00:15:03,050 about object oriented programming in Python see you in the next video 21119

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.