All language subtitles for Object Oriented Programming with Python - Full Course for Beginners

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian Download
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,080 --> 00:00:01,920 it's important for software developers 2 00:00:01,920 --> 00:00:03,760 to understand object-oriented 3 00:00:03,760 --> 00:00:04,880 programming 4 00:00:04,880 --> 00:00:06,960 in this course jim from gym-shaped 5 00:00:06,960 --> 00:00:08,559 coding will teach you all about 6 00:00:08,559 --> 00:00:10,880 object-oriented programming in python 7 00:00:10,880 --> 00:00:12,639 object-oriented programming it could be 8 00:00:12,639 --> 00:00:14,719 what is holding you back from being a 9 00:00:14,719 --> 00:00:17,279 great python developer and as well as 10 00:00:17,279 --> 00:00:19,920 landing your first job as a software 11 00:00:19,920 --> 00:00:21,039 engineer 12 00:00:21,039 --> 00:00:23,039 welcome everyone to python 13 00:00:23,039 --> 00:00:25,599 object-oriented programming course now 14 00:00:25,599 --> 00:00:27,279 if you struggle to understand the 15 00:00:27,279 --> 00:00:29,279 concepts of object-oriented programming 16 00:00:29,279 --> 00:00:31,439 in the past then you are totally fine 17 00:00:31,439 --> 00:00:33,200 and you are in good hands because in 18 00:00:33,200 --> 00:00:36,239 this course i'm going to make sure that 19 00:00:36,239 --> 00:00:37,920 this will be the last tutorial that you 20 00:00:37,920 --> 00:00:41,120 will ever watch about classes and the 21 00:00:41,120 --> 00:00:43,840 complex concepts that comes with object 22 00:00:43,840 --> 00:00:46,160 oriented programming and we are going to 23 00:00:46,160 --> 00:00:48,559 do this by developing a real python 24 00:00:48,559 --> 00:00:51,039 application that is going to be very 25 00:00:51,039 --> 00:00:53,920 cool to write and we will add to its 26 00:00:53,920 --> 00:00:56,879 complexity step by step and throughout 27 00:00:56,879 --> 00:00:58,559 the way we will understand everything 28 00:00:58,559 --> 00:01:00,239 that we need to know about 29 00:01:00,239 --> 00:01:02,320 object-oriented programming now there 30 00:01:02,320 --> 00:01:04,000 are going to be some requirements to 31 00:01:04,000 --> 00:01:06,320 this course i do expect from everybody 32 00:01:06,320 --> 00:01:08,479 to know at least about functions 33 00:01:08,479 --> 00:01:11,040 variables if statements and as well as 34 00:01:11,040 --> 00:01:13,520 for loops and if you know those things 35 00:01:13,520 --> 00:01:15,600 from other programming languages then 36 00:01:15,600 --> 00:01:17,759 this is also fine so with that being 37 00:01:17,759 --> 00:01:20,400 said let's get started now to explain 38 00:01:20,400 --> 00:01:22,080 why you should write object oriented 39 00:01:22,080 --> 00:01:24,400 programs i will explain the concepts 40 00:01:24,400 --> 00:01:26,560 based on a store management system that 41 00:01:26,560 --> 00:01:28,880 we will start developing together so 42 00:01:28,880 --> 00:01:30,720 starting to think about how to take our 43 00:01:30,720 --> 00:01:33,040 first steps with such a program we could 44 00:01:33,040 --> 00:01:34,720 first think about tracking after the 45 00:01:34,720 --> 00:01:36,640 items that we have right now in our 46 00:01:36,640 --> 00:01:39,200 store so one way we could get started we 47 00:01:39,200 --> 00:01:41,280 could create those four variables to 48 00:01:41,280 --> 00:01:43,840 start tracking after our items so as you 49 00:01:43,840 --> 00:01:46,079 can see we have our first variable item 50 00:01:46,079 --> 00:01:48,079 one equals to phone and then we have 51 00:01:48,079 --> 00:01:49,840 three more variables that are 52 00:01:49,840 --> 00:01:51,759 intentionally starting with the prefix 53 00:01:51,759 --> 00:01:54,320 of item one so that we could describe 54 00:01:54,320 --> 00:01:56,560 that those four variables are related to 55 00:01:56,560 --> 00:01:58,799 each other by following the correct 56 00:01:58,799 --> 00:02:00,960 naming conventions now you might think 57 00:02:00,960 --> 00:02:02,960 that those four variables are related to 58 00:02:02,960 --> 00:02:05,280 each other only because it uses the same 59 00:02:05,280 --> 00:02:08,239 prefix of item one for python those are 60 00:02:08,239 --> 00:02:10,560 just four variables with different data 61 00:02:10,560 --> 00:02:13,760 types so if we were to print the type 62 00:02:13,760 --> 00:02:16,239 for each of those four variables now we 63 00:02:16,239 --> 00:02:19,040 would receive their types with no 64 00:02:19,040 --> 00:02:21,920 surprises right we will receive string 65 00:02:21,920 --> 00:02:24,400 and integer for price quantity and price 66 00:02:24,400 --> 00:02:26,879 total now i want to focus on those 67 00:02:26,879 --> 00:02:29,200 specific outputs right now because as 68 00:02:29,200 --> 00:02:31,680 you can see for each of the types we 69 00:02:31,680 --> 00:02:35,200 also see the keyword of class now this 70 00:02:35,200 --> 00:02:39,120 means that those data types are actually 71 00:02:39,120 --> 00:02:43,360 instances of strings or integers so in 72 00:02:43,360 --> 00:02:45,760 python programming language each data 73 00:02:45,760 --> 00:02:47,920 type is an object that has been 74 00:02:47,920 --> 00:02:51,519 instantiated earlier by some class and 75 00:02:51,519 --> 00:02:54,400 for the item1 variable that has been 76 00:02:54,400 --> 00:02:57,440 instantiated from a string type of class 77 00:02:57,440 --> 00:02:59,440 and for the price quantity and price 78 00:02:59,440 --> 00:03:02,239 total those have been instantiated from 79 00:03:02,239 --> 00:03:04,959 a class that is named int meaning 80 00:03:04,959 --> 00:03:07,840 integer so it could have been nicer if 81 00:03:07,840 --> 00:03:10,239 we could tell python that we want to 82 00:03:10,239 --> 00:03:12,720 create a data type of our own it will 83 00:03:12,720 --> 00:03:14,640 allow us to write a code that we can 84 00:03:14,640 --> 00:03:17,840 reuse in the future easily if needed now 85 00:03:17,840 --> 00:03:20,000 each instance could have attributes to 86 00:03:20,000 --> 00:03:22,400 describe related information about it 87 00:03:22,400 --> 00:03:24,480 and we can think about at least some 88 00:03:24,480 --> 00:03:26,959 good candidates for attributes we could 89 00:03:26,959 --> 00:03:30,400 have for our item data type like its 90 00:03:30,400 --> 00:03:31,200 name 91 00:03:31,200 --> 00:03:34,080 price or quantity all right so let's go 92 00:03:34,080 --> 00:03:36,319 ahead and start creating our first class 93 00:03:36,319 --> 00:03:38,560 so i will clean everything from here and 94 00:03:38,560 --> 00:03:40,400 we'll go ahead with it so it is going to 95 00:03:40,400 --> 00:03:42,560 be divided into two parts the first one 96 00:03:42,560 --> 00:03:44,720 will be the creation of the class and 97 00:03:44,720 --> 00:03:47,280 the second one will be the part that i 98 00:03:47,280 --> 00:03:49,920 will instantiate some objects of this 99 00:03:49,920 --> 00:03:52,239 class now when i say creating an 100 00:03:52,239 --> 00:03:54,720 instance or creating an object basically 101 00:03:54,720 --> 00:03:56,799 i mean to the same thing so you might 102 00:03:56,799 --> 00:03:59,280 hear me saying one of those all right so 103 00:03:59,280 --> 00:04:01,920 let's go ahead and say class and then 104 00:04:01,920 --> 00:04:04,159 this needs to be followed by the name of 105 00:04:04,159 --> 00:04:06,080 the class that you want to create so we 106 00:04:06,080 --> 00:04:08,400 would like to give it the name of item 107 00:04:08,400 --> 00:04:10,640 and then inside of this class in the 108 00:04:10,640 --> 00:04:13,280 future we are going to write some code 109 00:04:13,280 --> 00:04:15,439 that will be very beneficial and very 110 00:04:15,439 --> 00:04:17,839 useful for us so we won't repeat 111 00:04:17,839 --> 00:04:19,839 ourselves every time that we like to 112 00:04:19,839 --> 00:04:22,400 take similar actions but for now 113 00:04:22,400 --> 00:04:24,560 temporarily i'm going to say here pass 114 00:04:24,560 --> 00:04:26,800 so we will not receive any errors inside 115 00:04:26,800 --> 00:04:28,960 this class definition all right so now 116 00:04:28,960 --> 00:04:31,120 that we have created our class then we 117 00:04:31,120 --> 00:04:32,960 are allowed to create some instances of 118 00:04:32,960 --> 00:04:34,960 this class so let's go ahead and say 119 00:04:34,960 --> 00:04:38,639 item one is equal to item and that 120 00:04:38,639 --> 00:04:41,120 action is equivalent to creating an 121 00:04:41,120 --> 00:04:43,600 instance of a class just like if you 122 00:04:43,600 --> 00:04:46,080 were to create a random string 123 00:04:46,080 --> 00:04:48,160 then you will say something like the 124 00:04:48,160 --> 00:04:51,280 following this is equivalent to this one 125 00:04:51,280 --> 00:04:53,280 as well so it is very important to 126 00:04:53,280 --> 00:04:56,080 understand how classes are working in 127 00:04:56,080 --> 00:04:57,840 python so i will delete this line 128 00:04:57,840 --> 00:05:00,720 because this was just for an example and 129 00:05:00,720 --> 00:05:02,960 now i said that we are allowed to assign 130 00:05:02,960 --> 00:05:05,759 some attributes to instances of a class 131 00:05:05,759 --> 00:05:08,160 so let's go ahead and start creating 132 00:05:08,160 --> 00:05:10,639 attributes and that will be achievable 133 00:05:10,639 --> 00:05:13,919 by using the dot sign right after the 134 00:05:13,919 --> 00:05:16,320 instance of a class and here you can say 135 00:05:16,320 --> 00:05:17,919 that you want to give it an attribute 136 00:05:17,919 --> 00:05:21,199 like a name that will be equal to phone 137 00:05:21,199 --> 00:05:24,479 and item1.price could be equal to 138 00:05:24,479 --> 00:05:28,880 100 and item one dot quantity could be 139 00:05:28,880 --> 00:05:29,919 equal to 140 00:05:29,919 --> 00:05:32,720 five for example now in that stage you 141 00:05:32,720 --> 00:05:34,479 might ask yourself what is the 142 00:05:34,479 --> 00:05:36,560 difference between the random variables 143 00:05:36,560 --> 00:05:39,120 that we have created to those four lines 144 00:05:39,120 --> 00:05:40,720 well here we actually have a 145 00:05:40,720 --> 00:05:43,440 relationship between those four lines 146 00:05:43,440 --> 00:05:45,680 because each one of the attributes are 147 00:05:45,680 --> 00:05:48,479 assigned to one instance of the class 148 00:05:48,479 --> 00:05:50,800 and i could preview this by going ahead 149 00:05:50,800 --> 00:05:54,560 and try to print the types of item one 150 00:05:54,560 --> 00:05:57,280 now and as well as the types of the 151 00:05:57,280 --> 00:05:58,960 attributes of 152 00:05:58,960 --> 00:06:01,520 name price and quantity now with name 153 00:06:01,520 --> 00:06:03,120 price and quantity we are not going to 154 00:06:03,120 --> 00:06:05,520 have any surprises because we assigned 155 00:06:05,520 --> 00:06:08,240 string type attributes to the item 156 00:06:08,240 --> 00:06:11,440 object but if we were to print that then 157 00:06:11,440 --> 00:06:13,600 check out the result if i was to run 158 00:06:13,600 --> 00:06:16,720 this program so you can see that now we 159 00:06:16,720 --> 00:06:19,520 have a data type of item here and that 160 00:06:19,520 --> 00:06:21,199 is the big difference between what we 161 00:06:21,199 --> 00:06:23,120 have seen previously to this thing that 162 00:06:23,120 --> 00:06:25,520 we have just created so now we 163 00:06:25,520 --> 00:06:27,199 understand how we can create our own 164 00:06:27,199 --> 00:06:29,440 data types now let's go ahead and see 165 00:06:29,440 --> 00:06:32,160 what are the rest of the benefits using 166 00:06:32,160 --> 00:06:34,400 object oriented programming okay so 167 00:06:34,400 --> 00:06:36,400 until now we understood how to assign 168 00:06:36,400 --> 00:06:39,199 attributes to instances we should also 169 00:06:39,199 --> 00:06:41,280 understand now how we can create some 170 00:06:41,280 --> 00:06:43,840 methods and execute them on our 171 00:06:43,840 --> 00:06:46,000 instances now if we will take as an 172 00:06:46,000 --> 00:06:48,800 example the built-in class of string 173 00:06:48,800 --> 00:06:50,720 then you know that we have some methods 174 00:06:50,720 --> 00:06:52,800 that we can go ahead and execute for 175 00:06:52,800 --> 00:06:55,599 each of our strings and for this example 176 00:06:55,599 --> 00:06:57,840 you can see that i grab an instance of a 177 00:06:57,840 --> 00:07:00,880 string that i named random str and then 178 00:07:00,880 --> 00:07:02,720 i go ahead in the next line and execute 179 00:07:02,720 --> 00:07:04,880 the upper method which if you remember 180 00:07:04,880 --> 00:07:07,360 is responsible to grab all the letters 181 00:07:07,360 --> 00:07:09,840 and turn them to uppercase now the 182 00:07:09,840 --> 00:07:11,759 biggest question here is how we can go 183 00:07:11,759 --> 00:07:14,800 ahead and design some methods that are 184 00:07:14,800 --> 00:07:16,880 going to be allowed to execute on our 185 00:07:16,880 --> 00:07:19,840 instances well the answer is inside our 186 00:07:19,840 --> 00:07:23,280 class so we could go inside our class 187 00:07:23,280 --> 00:07:25,199 and write some methods that will be 188 00:07:25,199 --> 00:07:28,720 accessible from our instances so we 189 00:07:28,720 --> 00:07:31,599 could go ahead and say def 190 00:07:31,599 --> 00:07:34,400 and give our method a name now a good 191 00:07:34,400 --> 00:07:36,080 candidate for a method that we'd like to 192 00:07:36,080 --> 00:07:39,759 create now is actually calculate total 193 00:07:39,759 --> 00:07:42,000 price because as we understand it could 194 00:07:42,000 --> 00:07:43,759 have been nice if we were to have a 195 00:07:43,759 --> 00:07:45,840 method that will go ahead and calculate 196 00:07:45,840 --> 00:07:49,639 the result multiplying item1.price with 197 00:07:49,639 --> 00:07:52,400 item1.quantity so we can get the total 198 00:07:52,400 --> 00:07:54,879 price for that specific item now before 199 00:07:54,879 --> 00:07:57,360 we go ahead and complete this function 200 00:07:57,360 --> 00:07:59,199 then i'm going to just create one more 201 00:07:59,199 --> 00:08:01,919 instance of this item by also deleting 202 00:08:01,919 --> 00:08:03,680 those two lines because we understood 203 00:08:03,680 --> 00:08:05,599 the example so i'm just going to change 204 00:08:05,599 --> 00:08:07,599 those to item 2 205 00:08:07,599 --> 00:08:10,080 like that and i'm going to use something 206 00:08:10,080 --> 00:08:10,960 like 207 00:08:10,960 --> 00:08:13,919 laptop and change the price to 1000 and 208 00:08:13,919 --> 00:08:16,879 say that we have three of those now just 209 00:08:16,879 --> 00:08:18,479 a quick side note when you will hear me 210 00:08:18,479 --> 00:08:21,199 say methods then i basically mean to 211 00:08:21,199 --> 00:08:23,520 functions that are inside classes 212 00:08:23,520 --> 00:08:25,759 because in terms of python or in any 213 00:08:25,759 --> 00:08:27,360 programming language when you have 214 00:08:27,360 --> 00:08:30,720 isolated definitions with this keyword 215 00:08:30,720 --> 00:08:32,559 then those are considered to be called 216 00:08:32,559 --> 00:08:34,399 functions but when you go ahead and 217 00:08:34,399 --> 00:08:37,279 create those functions inside classes 218 00:08:37,279 --> 00:08:39,919 then those are called methods so that is 219 00:08:39,919 --> 00:08:41,679 an important point that you should 220 00:08:41,679 --> 00:08:43,440 understand because i'm going to call 221 00:08:43,440 --> 00:08:46,080 those methods from now okay so now if i 222 00:08:46,080 --> 00:08:48,560 was to continue by opening up and 223 00:08:48,560 --> 00:08:50,640 closing those parentheses then you are 224 00:08:50,640 --> 00:08:53,040 going to see one parameter that is 225 00:08:53,040 --> 00:08:55,680 generated that python wants us to 226 00:08:55,680 --> 00:08:57,839 receive intentionally now the reason 227 00:08:57,839 --> 00:08:59,360 that this happens 228 00:08:59,360 --> 00:09:00,480 python 229 00:09:00,480 --> 00:09:03,600 passes the object itself as a first 230 00:09:03,600 --> 00:09:06,160 argument when you go ahead and call 231 00:09:06,160 --> 00:09:10,080 those methods now if i was to go here 232 00:09:10,080 --> 00:09:14,240 and say item1 dot calculate total price 233 00:09:14,240 --> 00:09:16,640 then the action that we are doing now is 234 00:09:16,640 --> 00:09:18,560 calling this method but when you go 235 00:09:18,560 --> 00:09:21,839 ahead and call a method from an instance 236 00:09:21,839 --> 00:09:25,600 then python passes the object itself as 237 00:09:25,600 --> 00:09:28,399 a first argument every time so that is 238 00:09:28,399 --> 00:09:30,880 why we are not allowed to create methods 239 00:09:30,880 --> 00:09:33,600 that will never receive parameters now 240 00:09:33,600 --> 00:09:36,160 you will see this if i was to remove the 241 00:09:36,160 --> 00:09:38,800 first parameter and say something like 242 00:09:38,800 --> 00:09:42,480 pass now if i was to execute 243 00:09:42,480 --> 00:09:44,560 this program now then you are going to 244 00:09:44,560 --> 00:09:47,839 see type error calculate total price 245 00:09:47,839 --> 00:09:51,440 takes 0 positional arguments but one was 246 00:09:51,440 --> 00:09:53,920 given so in simple words what this 247 00:09:53,920 --> 00:09:57,120 exception says is that python tries to 248 00:09:57,120 --> 00:09:59,600 pass one argument and you are not 249 00:09:59,600 --> 00:10:01,839 receiving any parameter so that is very 250 00:10:01,839 --> 00:10:04,640 problematic and that is why you always 251 00:10:04,640 --> 00:10:07,040 have to receive at least one parameter 252 00:10:07,040 --> 00:10:08,320 when you go ahead and create your 253 00:10:08,320 --> 00:10:11,760 methods now since we always receive this 254 00:10:11,760 --> 00:10:14,079 parameter then it is just a common 255 00:10:14,079 --> 00:10:17,040 approach to call this self it was okay 256 00:10:17,040 --> 00:10:19,279 if i was to call it something like my 257 00:10:19,279 --> 00:10:22,240 param or i don't know something else but 258 00:10:22,240 --> 00:10:24,320 you never want to mess up with common 259 00:10:24,320 --> 00:10:26,399 conventions across different python 260 00:10:26,399 --> 00:10:28,640 developers so that is why just make sure 261 00:10:28,640 --> 00:10:32,399 that you leave it as self every time now 262 00:10:32,399 --> 00:10:35,120 if i was to go ahead and 263 00:10:35,120 --> 00:10:36,959 run this program then you're gonna see 264 00:10:36,959 --> 00:10:38,480 that we are not going to receive any 265 00:10:38,480 --> 00:10:40,240 errors so this means that this method 266 00:10:40,240 --> 00:10:42,800 has been implemented correctly now let's 267 00:10:42,800 --> 00:10:44,800 see how we are going to benefit from 268 00:10:44,800 --> 00:10:46,880 creating this method because it should 269 00:10:46,880 --> 00:10:49,680 go ahead and create a calculation for us 270 00:10:49,680 --> 00:10:52,480 using price and quantity so i will 271 00:10:52,480 --> 00:10:54,880 intentionally receive here two more 272 00:10:54,880 --> 00:10:57,200 parameters which we could name just x 273 00:10:57,200 --> 00:10:59,920 and y for now and we could just say 274 00:10:59,920 --> 00:11:04,240 return x multiplied by y and now i will 275 00:11:04,240 --> 00:11:06,640 go ahead and pass in here two additional 276 00:11:06,640 --> 00:11:11,279 arguments and it will be item1.price 277 00:11:11,279 --> 00:11:14,240 the second one will be quantity so that 278 00:11:14,240 --> 00:11:16,079 is going to work because when you call 279 00:11:16,079 --> 00:11:18,640 this method in the background python 280 00:11:18,640 --> 00:11:20,959 passes this as an argument and then it 281 00:11:20,959 --> 00:11:23,360 passes this second argument and then 282 00:11:23,360 --> 00:11:25,360 this has been passed as a third argument 283 00:11:25,360 --> 00:11:27,519 so that is perfect and if i was to run 284 00:11:27,519 --> 00:11:30,480 that and actually print this so excuse 285 00:11:30,480 --> 00:11:31,760 me for running this 286 00:11:31,760 --> 00:11:34,240 before printing it so i will 287 00:11:34,240 --> 00:11:36,160 surround this 288 00:11:36,160 --> 00:11:38,240 expression with this print built-in 289 00:11:38,240 --> 00:11:40,160 function and i will run that and you're 290 00:11:40,160 --> 00:11:43,440 gonna see 500 as expected now i could do 291 00:11:43,440 --> 00:11:46,000 the exact same thing for calculating the 292 00:11:46,000 --> 00:11:49,360 total price of our second item so if i 293 00:11:49,360 --> 00:11:52,560 was to grab this and paste this in in 294 00:11:52,560 --> 00:11:55,040 this line and actually change this to 295 00:11:55,040 --> 00:11:58,639 item two and change this one to item two 296 00:11:58,639 --> 00:12:01,120 and as well as this one then i will 297 00:12:01,120 --> 00:12:02,639 receive 298 00:12:02,639 --> 00:12:05,519 three thousand as expected and that is 299 00:12:05,519 --> 00:12:07,600 how you can create a metal alright so 300 00:12:07,600 --> 00:12:09,680 until that point we understood that we 301 00:12:09,680 --> 00:12:11,680 can assign attributes and as well as 302 00:12:11,680 --> 00:12:14,000 creating some methods that we can go 303 00:12:14,000 --> 00:12:16,000 ahead and use them from our instances 304 00:12:16,000 --> 00:12:19,279 directly like those two examples in that 305 00:12:19,279 --> 00:12:22,560 line and as well as in that line now in 306 00:12:22,560 --> 00:12:24,560 that episode we are going to solve some 307 00:12:24,560 --> 00:12:26,639 more problems that we have in terms of 308 00:12:26,639 --> 00:12:28,480 best practices in object-oriented 309 00:12:28,480 --> 00:12:30,160 programming and things that you are 310 00:12:30,160 --> 00:12:32,079 going to see in each project that is 311 00:12:32,079 --> 00:12:34,720 based on oop all right so let's get 312 00:12:34,720 --> 00:12:36,480 started now one of the first problems 313 00:12:36,480 --> 00:12:38,800 that we have here is the fact that we 314 00:12:38,800 --> 00:12:41,279 don't have a set of rules for the 315 00:12:41,279 --> 00:12:43,040 attributes that you would like to pass 316 00:12:43,040 --> 00:12:44,880 in in order to instantiate an instance 317 00:12:44,880 --> 00:12:47,040 successfully and what that means it 318 00:12:47,040 --> 00:12:49,360 means that for each item that i want to 319 00:12:49,360 --> 00:12:51,600 go ahead and create i need to hard code 320 00:12:51,600 --> 00:12:53,519 in the attribute name 321 00:12:53,519 --> 00:12:56,639 like those in here and it could have 322 00:12:56,639 --> 00:12:59,200 been nicer if we could somehow declare 323 00:12:59,200 --> 00:13:01,040 in the class that in order to 324 00:13:01,040 --> 00:13:03,519 instantiate an instance successfully 325 00:13:03,519 --> 00:13:06,959 name price and quantity must be passed 326 00:13:06,959 --> 00:13:08,880 otherwise the instance could not have 327 00:13:08,880 --> 00:13:11,200 been created successfully so what that 328 00:13:11,200 --> 00:13:13,519 means it means that it could have been a 329 00:13:13,519 --> 00:13:16,560 great option if we could somehow execute 330 00:13:16,560 --> 00:13:19,040 something in the background this second 331 00:13:19,040 --> 00:13:21,519 that we instantiate an instance and 332 00:13:21,519 --> 00:13:24,000 there is a way that you can reach such a 333 00:13:24,000 --> 00:13:26,399 behavior and that is by creating a 334 00:13:26,399 --> 00:13:28,959 special method with a very unique name 335 00:13:28,959 --> 00:13:31,120 which is called double underscore init 336 00:13:31,120 --> 00:13:33,760 double underscore now you might hear 337 00:13:33,760 --> 00:13:35,920 this term as well as called as 338 00:13:35,920 --> 00:13:38,639 constructor basically that is a method 339 00:13:38,639 --> 00:13:41,199 with a unique name that you need to call 340 00:13:41,199 --> 00:13:44,000 it the way it is intentionally in order 341 00:13:44,000 --> 00:13:47,279 to use its special features now the way 342 00:13:47,279 --> 00:13:49,440 that this is going to work is by 343 00:13:49,440 --> 00:13:50,720 creating 344 00:13:50,720 --> 00:13:52,959 it the following way so it will be 345 00:13:52,959 --> 00:13:56,079 double underscore and as you can see i 346 00:13:56,079 --> 00:13:58,079 already have auto completion for some 347 00:13:58,079 --> 00:14:00,639 very special methods that are starting 348 00:14:00,639 --> 00:14:03,120 and ending with double underscore now 349 00:14:03,120 --> 00:14:06,160 the collection of those methods are used 350 00:14:06,160 --> 00:14:08,480 to be called magic methods and we are 351 00:14:08,480 --> 00:14:10,399 going to learn a lot of more magic 352 00:14:10,399 --> 00:14:13,680 methods that you have in oop but the 353 00:14:13,680 --> 00:14:15,120 first one that we are going to start 354 00:14:15,120 --> 00:14:18,399 with will be the init double underscore 355 00:14:18,399 --> 00:14:20,560 like that all right so now that we have 356 00:14:20,560 --> 00:14:22,880 created this method then let's actually 357 00:14:22,880 --> 00:14:24,480 see what this method does in the 358 00:14:24,480 --> 00:14:26,399 background so when you go ahead and 359 00:14:26,399 --> 00:14:29,040 create an instance of a class then 360 00:14:29,040 --> 00:14:31,440 python executes this double underscore 361 00:14:31,440 --> 00:14:34,320 init function automatically so what that 362 00:14:34,320 --> 00:14:36,560 means it means that now that we have 363 00:14:36,560 --> 00:14:38,959 declared our class python is going to 364 00:14:38,959 --> 00:14:41,360 run through this line and then since an 365 00:14:41,360 --> 00:14:43,760 instance has been created and we have 366 00:14:43,760 --> 00:14:46,160 double score init method designed then 367 00:14:46,160 --> 00:14:48,399 it is going to call the actions that are 368 00:14:48,399 --> 00:14:50,480 inside this double underscore init 369 00:14:50,480 --> 00:14:52,880 double underscore method now in order to 370 00:14:52,880 --> 00:14:55,360 prove you that then i'm going to start 371 00:14:55,360 --> 00:14:58,079 with a very basic print here that will 372 00:14:58,079 --> 00:15:00,399 say i am created 373 00:15:00,399 --> 00:15:04,079 like that now we got here one instance 374 00:15:04,079 --> 00:15:06,720 and here we got another one so we should 375 00:15:06,720 --> 00:15:09,360 see i am created twice and in order to 376 00:15:09,360 --> 00:15:11,199 avoid confusions then i'm going to 377 00:15:11,199 --> 00:15:13,920 delete those print lines from here so we 378 00:15:13,920 --> 00:15:16,959 can see a cleaner picture all right so 379 00:15:16,959 --> 00:15:19,279 if we were to run our program then you 380 00:15:19,279 --> 00:15:21,920 can see that we have im created twice 381 00:15:21,920 --> 00:15:24,560 and that is because python called this 382 00:15:24,560 --> 00:15:26,160 double underscore init double underscore 383 00:15:26,160 --> 00:15:28,480 method twice thanks to those two 384 00:15:28,480 --> 00:15:30,480 instances that we have created all right 385 00:15:30,480 --> 00:15:32,160 so now that we use the double underscore 386 00:15:32,160 --> 00:15:34,160 init function in this class we should 387 00:15:34,160 --> 00:15:36,480 take benefit from it and solve some more 388 00:15:36,480 --> 00:15:39,040 problems in order to implement oop best 389 00:15:39,040 --> 00:15:41,120 practices now if you remember in the 390 00:15:41,120 --> 00:15:43,120 beginning of this tutorial i said that 391 00:15:43,120 --> 00:15:44,800 one of the problems that we have till 392 00:15:44,800 --> 00:15:46,959 this point is the fact that we still 393 00:15:46,959 --> 00:15:49,519 hard code in the attributes in that way 394 00:15:49,519 --> 00:15:52,079 by saying dot name dot price dot 395 00:15:52,079 --> 00:15:54,639 quantity and that is something that we 396 00:15:54,639 --> 00:15:57,519 can for sure avoid now let's see how we 397 00:15:57,519 --> 00:15:59,519 can start avoiding creating those 398 00:15:59,519 --> 00:16:01,440 attributes hard-coded for each of the 399 00:16:01,440 --> 00:16:03,279 instances here so we can actually 400 00:16:03,279 --> 00:16:04,959 benefit from the double underscore init 401 00:16:04,959 --> 00:16:06,959 method that we have designed and let's 402 00:16:06,959 --> 00:16:09,759 see how now we understand that for each 403 00:16:09,759 --> 00:16:12,000 instance that we will create it will go 404 00:16:12,000 --> 00:16:13,519 ahead and call this double underscore 405 00:16:13,519 --> 00:16:16,000 init method automatically so what that 406 00:16:16,000 --> 00:16:18,720 means it means that not only we can 407 00:16:18,720 --> 00:16:20,320 allow ourselves to receive the sales 408 00:16:20,320 --> 00:16:22,160 parameter because this is a mandatory 409 00:16:22,160 --> 00:16:24,000 thing that we should do because python 410 00:16:24,000 --> 00:16:26,399 in the background passes the instance 411 00:16:26,399 --> 00:16:28,959 itself as a first argument we could in 412 00:16:28,959 --> 00:16:32,000 addition take some more parameters and 413 00:16:32,000 --> 00:16:34,560 then do something with them so as a 414 00:16:34,560 --> 00:16:36,639 starter let's say that we would like to 415 00:16:36,639 --> 00:16:38,560 receive one more parameter that we could 416 00:16:38,560 --> 00:16:41,120 name it name and as you can see 417 00:16:41,120 --> 00:16:43,120 automatically python is going to 418 00:16:43,120 --> 00:16:46,320 complain how the name argument is not 419 00:16:46,320 --> 00:16:48,240 filled in here so 420 00:16:48,240 --> 00:16:50,720 now i could go ahead and pass in the 421 00:16:50,720 --> 00:16:53,279 argument of phone for that one and for 422 00:16:53,279 --> 00:16:55,519 the second one i can go ahead and pass 423 00:16:55,519 --> 00:16:58,800 in the argument of laptop now once i 424 00:16:58,800 --> 00:17:01,279 have created this then i can actually go 425 00:17:01,279 --> 00:17:03,120 ahead and change my print line a little 426 00:17:03,120 --> 00:17:05,599 bit so it will be a unique print line 427 00:17:05,599 --> 00:17:08,240 where i can identify from where each 428 00:17:08,240 --> 00:17:10,480 print line came from so i can go ahead 429 00:17:10,480 --> 00:17:12,640 and say an instance 430 00:17:12,640 --> 00:17:13,839 created 431 00:17:13,839 --> 00:17:16,720 and use a column here and then refer to 432 00:17:16,720 --> 00:17:19,760 the name like that and now that we have 433 00:17:19,760 --> 00:17:22,480 created this then if we were to run our 434 00:17:22,480 --> 00:17:24,720 program then you're gonna see unique 435 00:17:24,720 --> 00:17:27,039 sentences and instances created for the 436 00:17:27,039 --> 00:17:30,000 phone and as well as for the laptop all 437 00:17:30,000 --> 00:17:32,320 right so now that we have done this then 438 00:17:32,320 --> 00:17:34,000 there is something that is still not 439 00:17:34,000 --> 00:17:36,640 quite perfect because we still pass in 440 00:17:36,640 --> 00:17:40,080 the attribute of name here and here so 441 00:17:40,080 --> 00:17:43,120 now pay attention how the init method 442 00:17:43,120 --> 00:17:45,760 has to receive the self as a perimeter 443 00:17:45,760 --> 00:17:47,760 as well and we already know the reason 444 00:17:47,760 --> 00:17:50,080 for that and the fact that we have self 445 00:17:50,080 --> 00:17:52,320 as a parameter here could actually allow 446 00:17:52,320 --> 00:17:54,480 us to assign the attributes from the 447 00:17:54,480 --> 00:17:57,039 init method so that we will not have to 448 00:17:57,039 --> 00:17:59,039 go ahead and assign the attribute of 449 00:17:59,039 --> 00:18:01,919 name for each of the instances we create 450 00:18:01,919 --> 00:18:04,160 so what that means it means that i can 451 00:18:04,160 --> 00:18:07,039 dynamically assign an attribute to an 452 00:18:07,039 --> 00:18:09,679 instance from this magic method which is 453 00:18:09,679 --> 00:18:12,320 called double underscore init so if i 454 00:18:12,320 --> 00:18:14,880 was to say self 455 00:18:14,880 --> 00:18:18,240 dot name so i'm assigning the attribute 456 00:18:18,240 --> 00:18:21,760 of name to each instance that is going 457 00:18:21,760 --> 00:18:24,640 to be created or created yet and i'm 458 00:18:24,640 --> 00:18:27,520 making that to be equal to the name 459 00:18:27,520 --> 00:18:30,400 that is passed in from here so what that 460 00:18:30,400 --> 00:18:32,720 means it means that now i can allow 461 00:18:32,720 --> 00:18:34,640 myself to delete 462 00:18:34,640 --> 00:18:37,280 this line and then 463 00:18:37,280 --> 00:18:40,160 this line so as you can see now i have a 464 00:18:40,160 --> 00:18:42,880 dynamic attribute assignment thanks to 465 00:18:42,880 --> 00:18:45,280 this self.name equals name that we have 466 00:18:45,280 --> 00:18:47,520 wrote here and to test that the 467 00:18:47,520 --> 00:18:49,679 attribute assignment worked then i can 468 00:18:49,679 --> 00:18:52,080 go down here and use two more lines that 469 00:18:52,080 --> 00:18:53,760 will look like the following so i will 470 00:18:53,760 --> 00:18:55,799 print item 471 00:18:55,799 --> 00:18:59,600 item1.name and i will also print item2 472 00:18:59,600 --> 00:19:02,000 dot name and in order to avoid 473 00:19:02,000 --> 00:19:04,080 confusions then i'm going to get rid of 474 00:19:04,080 --> 00:19:06,480 this line so we could only see the print 475 00:19:06,480 --> 00:19:08,799 lines from here and now if i was to run 476 00:19:08,799 --> 00:19:10,880 that then you can see that we receive 477 00:19:10,880 --> 00:19:13,440 phone and laptop so it means that we 478 00:19:13,440 --> 00:19:15,280 were able to assign the attributes 479 00:19:15,280 --> 00:19:18,080 dynamically and that is perfect and now 480 00:19:18,080 --> 00:19:20,080 that we get the idea of that then we 481 00:19:20,080 --> 00:19:21,840 should also do the same for the rest of 482 00:19:21,840 --> 00:19:24,240 the attributes that we'd like to receive 483 00:19:24,240 --> 00:19:27,120 so we also got the price and quantity to 484 00:19:27,120 --> 00:19:29,919 take care of so i'm going to go to my 485 00:19:29,919 --> 00:19:31,520 init method and i'm going to receive 486 00:19:31,520 --> 00:19:32,400 again 487 00:19:32,400 --> 00:19:33,360 price 488 00:19:33,360 --> 00:19:35,679 and quantity and i'm going to do the 489 00:19:35,679 --> 00:19:37,840 exact same thing so i'm going to assign 490 00:19:37,840 --> 00:19:40,320 the attribute of price and that will be 491 00:19:40,320 --> 00:19:41,440 equal to 492 00:19:41,440 --> 00:19:44,720 price and the quantity will be equal to 493 00:19:44,720 --> 00:19:46,960 the quantity and you can also see that 494 00:19:46,960 --> 00:19:49,200 again python complains about the price 495 00:19:49,200 --> 00:19:51,200 and the quantity not being passed in 496 00:19:51,200 --> 00:19:55,200 here so i can say 100 and then 5 497 00:19:55,200 --> 00:19:57,200 and then i can delete those 498 00:19:57,200 --> 00:19:59,760 and then i can do the same here i could 499 00:19:59,760 --> 00:20:04,640 pass in 1000 and then 3 and delete those 500 00:20:04,640 --> 00:20:06,400 and in order to prove you that this is 501 00:20:06,400 --> 00:20:09,200 going to work then i'm going to copy 502 00:20:09,200 --> 00:20:11,600 myself a couple of times and change this 503 00:20:11,600 --> 00:20:14,720 to quantity i mean 504 00:20:14,720 --> 00:20:16,159 price 505 00:20:16,159 --> 00:20:18,559 this one will be price as well 506 00:20:18,559 --> 00:20:21,280 this one will be quantity and this one 507 00:20:21,280 --> 00:20:23,919 as well and now if i was to run that 508 00:20:23,919 --> 00:20:25,919 then you can see that the results are as 509 00:20:25,919 --> 00:20:27,840 expected so that is a way that you 510 00:20:27,840 --> 00:20:30,320 should work with the double underscore 511 00:20:30,320 --> 00:20:32,559 init method you should always take care 512 00:20:32,559 --> 00:20:34,159 of the attributes that you want to 513 00:20:34,159 --> 00:20:36,559 assign to an object inside the double 514 00:20:36,559 --> 00:20:38,880 underscore init method meaning inside 515 00:20:38,880 --> 00:20:41,039 the constructor now a couple of side 516 00:20:41,039 --> 00:20:42,480 notes that are quite important to 517 00:20:42,480 --> 00:20:45,120 remember when we work with classes now 518 00:20:45,120 --> 00:20:46,720 when we go ahead and use the double 519 00:20:46,720 --> 00:20:48,960 score init method this doesn't mean that 520 00:20:48,960 --> 00:20:50,960 we cannot differentiate between 521 00:20:50,960 --> 00:20:53,679 mandatory parameters to non-mandatory 522 00:20:53,679 --> 00:20:56,640 parameters so say that you currently 523 00:20:56,640 --> 00:20:58,799 don't know how much you have from a 524 00:20:58,799 --> 00:21:01,840 specific item then you can go ahead and 525 00:21:01,840 --> 00:21:04,320 by default receive this quantity 526 00:21:04,320 --> 00:21:06,320 parameter as zero because it is 527 00:21:06,320 --> 00:21:08,080 realistic situation that you currently 528 00:21:08,080 --> 00:21:09,919 don't know how much phones you have on 529 00:21:09,919 --> 00:21:12,240 your store so you can directly go ahead 530 00:21:12,240 --> 00:21:14,640 and use a default value for that for 531 00:21:14,640 --> 00:21:15,600 example 532 00:21:15,600 --> 00:21:18,159 zero and then this will mean that you 533 00:21:18,159 --> 00:21:21,039 will not have to pass in those five and 534 00:21:21,039 --> 00:21:23,520 three here and now in order to show you 535 00:21:23,520 --> 00:21:26,080 the results of that if i was to run our 536 00:21:26,080 --> 00:21:27,840 program then you can see that we 537 00:21:27,840 --> 00:21:31,440 received zero twice for those two prints 538 00:21:31,440 --> 00:21:33,440 in here so that is something that you 539 00:21:33,440 --> 00:21:35,760 want to remember and one more quite 540 00:21:35,760 --> 00:21:37,840 important point that i'd like to talk 541 00:21:37,840 --> 00:21:40,000 about now is the fact that you can 542 00:21:40,000 --> 00:21:43,280 assign attributes to specific instances 543 00:21:43,280 --> 00:21:45,919 individually so say that you want to 544 00:21:45,919 --> 00:21:49,440 know if the laptop has numpad or not 545 00:21:49,440 --> 00:21:52,320 because some laptops are not having the 546 00:21:52,320 --> 00:21:54,880 numpad on the right side of the keyboard 547 00:21:54,880 --> 00:21:57,760 but this is not a realistic attribute 548 00:21:57,760 --> 00:22:00,559 that you would want to assign to a phone 549 00:22:00,559 --> 00:22:03,440 and that is why you can go ahead and let 550 00:22:03,440 --> 00:22:05,919 me delete those print lines by the way 551 00:22:05,919 --> 00:22:07,840 and that is why you can go ahead and say 552 00:22:07,840 --> 00:22:11,120 something like item two dot has 553 00:22:11,120 --> 00:22:14,320 numpad equals to false like that and 554 00:22:14,320 --> 00:22:15,760 that is something that you want to 555 00:22:15,760 --> 00:22:18,480 remember because the fact that you use 556 00:22:18,480 --> 00:22:20,480 some attribute assignments in the 557 00:22:20,480 --> 00:22:23,039 constructor doesn't mean that you cannot 558 00:22:23,039 --> 00:22:24,960 add some more attributes that you would 559 00:22:24,960 --> 00:22:28,159 like after you instantiate the instances 560 00:22:28,159 --> 00:22:30,400 that you would like to alright so now 561 00:22:30,400 --> 00:22:32,400 that we understood this then there is 562 00:22:32,400 --> 00:22:34,480 still one small problem that is left 563 00:22:34,480 --> 00:22:37,039 that we need to solve now pay attention 564 00:22:37,039 --> 00:22:40,000 how the calculate total price still 565 00:22:40,000 --> 00:22:43,200 receives the x and y as parameters and 566 00:22:43,200 --> 00:22:45,760 the question that we ask now is 567 00:22:45,760 --> 00:22:48,159 why it still receives those parameters 568 00:22:48,159 --> 00:22:50,960 well we could for sure now not receive 569 00:22:50,960 --> 00:22:53,679 those parameters because as we know for 570 00:22:53,679 --> 00:22:56,240 each method that we design in classes 571 00:22:56,240 --> 00:22:58,720 then the object itself is passed in 572 00:22:58,720 --> 00:23:00,559 argument and i know that i repeated this 573 00:23:00,559 --> 00:23:02,960 couple of times but this is where i fail 574 00:23:02,960 --> 00:23:05,360 to understand classes so that is why it 575 00:23:05,360 --> 00:23:07,520 is very important to understand this 576 00:23:07,520 --> 00:23:09,760 behavior and we already know that the 577 00:23:09,760 --> 00:23:12,080 object itself passed as an argument so 578 00:23:12,080 --> 00:23:15,280 that's why we receive self and so this 579 00:23:15,280 --> 00:23:19,200 means that now we could just return 580 00:23:19,200 --> 00:23:23,200 self.price multiplied by self.quantity 581 00:23:23,200 --> 00:23:25,039 and this will mean that we don't really 582 00:23:25,039 --> 00:23:27,440 have to receive those parameters because 583 00:23:27,440 --> 00:23:29,679 we assign those attributes 584 00:23:29,679 --> 00:23:32,320 once the instances has been created so 585 00:23:32,320 --> 00:23:34,640 this means that we have access to those 586 00:23:34,640 --> 00:23:36,559 attributes throughout the methods that 587 00:23:36,559 --> 00:23:38,400 we are going to add here in this class 588 00:23:38,400 --> 00:23:40,720 in the future so in order to test that 589 00:23:40,720 --> 00:23:43,039 this works then i'm going to 590 00:23:43,039 --> 00:23:45,440 delete this example for now and i'm 591 00:23:45,440 --> 00:23:48,080 going to say print item 1 592 00:23:48,080 --> 00:23:48,960 dot 593 00:23:48,960 --> 00:23:51,840 calculate total price so we will be able 594 00:23:51,840 --> 00:23:54,880 to return the result here and i will do 595 00:23:54,880 --> 00:23:56,400 the same for 596 00:23:56,400 --> 00:23:57,440 item 597 00:23:57,440 --> 00:24:00,799 two um sorry only this one now to show 598 00:24:00,799 --> 00:24:03,760 some real number other than zero then i 599 00:24:03,760 --> 00:24:06,320 will go ahead and pass in here 600 00:24:06,320 --> 00:24:09,039 quantities so i will say one and 601 00:24:09,039 --> 00:24:10,880 three for example because i don't want 602 00:24:10,880 --> 00:24:13,360 to multiply a large number with a zero 603 00:24:13,360 --> 00:24:16,400 and that could come from here so i will 604 00:24:16,400 --> 00:24:18,720 run that and you see that we receive the 605 00:24:18,720 --> 00:24:21,760 expected results so now we completely 606 00:24:21,760 --> 00:24:24,640 understand the big picture how to work 607 00:24:24,640 --> 00:24:27,120 with the constructors in classes and 608 00:24:27,120 --> 00:24:28,960 what are the best practices that you 609 00:24:28,960 --> 00:24:31,279 should go ahead and implement all right 610 00:24:31,279 --> 00:24:33,760 so now that we understood this then we 611 00:24:33,760 --> 00:24:35,440 might think that we have done everything 612 00:24:35,440 --> 00:24:37,760 perfectly but actually i want to show 613 00:24:37,760 --> 00:24:40,320 you what will happen if we were to pass 614 00:24:40,320 --> 00:24:43,279 in here a string besides 615 00:24:43,279 --> 00:24:46,640 an integer and run our program so if we 616 00:24:46,640 --> 00:24:48,640 were to run that then you can see that 617 00:24:48,640 --> 00:24:51,120 we are screwing things up here because 618 00:24:51,120 --> 00:24:53,600 this function for example thinks that it 619 00:24:53,600 --> 00:24:56,480 should print the string 620 00:24:56,480 --> 00:24:58,480 three times because you'll see we have 621 00:24:58,480 --> 00:25:01,600 1000 multiplied by three that is being 622 00:25:01,600 --> 00:25:03,760 returned in here so it shows us one 623 00:25:03,760 --> 00:25:05,120 thousand once 624 00:25:05,120 --> 00:25:06,400 one thousand 625 00:25:06,400 --> 00:25:09,039 twice and then one more time so what 626 00:25:09,039 --> 00:25:11,200 that means it means that we have to 627 00:25:11,200 --> 00:25:13,679 validate the data types of the values 628 00:25:13,679 --> 00:25:16,080 that we are passing in so there are a 629 00:25:16,080 --> 00:25:18,640 couple of ways to achieve this and one 630 00:25:18,640 --> 00:25:20,799 way is by using typings in the 631 00:25:20,799 --> 00:25:23,200 parameters that you're declaring inside 632 00:25:23,200 --> 00:25:26,240 here so a great starter will be for 633 00:25:26,240 --> 00:25:28,880 example to declare that a name must be a 634 00:25:28,880 --> 00:25:32,320 string now let me first take this back 635 00:25:32,320 --> 00:25:35,039 and change those to integer and then go 636 00:25:35,039 --> 00:25:38,320 here and design those parameters so in 637 00:25:38,320 --> 00:25:40,559 order to specify a typing then you 638 00:25:40,559 --> 00:25:42,799 should go ahead and create a column sign 639 00:25:42,799 --> 00:25:45,039 followed by the type of the data type 640 00:25:45,039 --> 00:25:47,760 that you expect to receive here so if i 641 00:25:47,760 --> 00:25:50,159 was to pass in here only the object 642 00:25:50,159 --> 00:25:53,600 reference to the class of str then it 643 00:25:53,600 --> 00:25:56,080 will mean that it will have to accept 644 00:25:56,080 --> 00:25:58,960 strings only and i can preview that by 645 00:25:58,960 --> 00:26:01,200 changing this to an integer and you're 646 00:26:01,200 --> 00:26:03,440 gonna see that we have a complaint here 647 00:26:03,440 --> 00:26:07,039 that says expected type str got int 648 00:26:07,039 --> 00:26:10,320 instead and that is perfect so now that 649 00:26:10,320 --> 00:26:12,240 we have done this then i'm going to do 650 00:26:12,240 --> 00:26:15,760 the same for the price itself and price 651 00:26:15,760 --> 00:26:17,200 we could actually 652 00:26:17,200 --> 00:26:19,279 do the same thing with it 653 00:26:19,279 --> 00:26:22,960 by passing in float now when we pass 654 00:26:22,960 --> 00:26:26,559 float it is okay to also pass integers 655 00:26:26,559 --> 00:26:28,240 and that is something very unique with 656 00:26:28,240 --> 00:26:30,400 floats and integers together so that is 657 00:26:30,400 --> 00:26:33,760 okay to use the typing of float and for 658 00:26:33,760 --> 00:26:36,480 the quantity we don't need to specify a 659 00:26:36,480 --> 00:26:39,279 typing because the fact that we passed a 660 00:26:39,279 --> 00:26:42,159 default value of integer already marked 661 00:26:42,159 --> 00:26:45,360 this parameter as to be integer always 662 00:26:45,360 --> 00:26:48,640 so that is why for example if i was to 663 00:26:48,640 --> 00:26:50,640 leave this as it is and change the 664 00:26:50,640 --> 00:26:53,279 quantity to a string then you're gonna 665 00:26:53,279 --> 00:26:55,440 see that it is going to complain because 666 00:26:55,440 --> 00:26:58,080 the default value is already an integer 667 00:26:58,080 --> 00:27:01,279 so it expects for an integer all right 668 00:27:01,279 --> 00:27:03,120 so those things are actually great 669 00:27:03,120 --> 00:27:06,000 setups to make our init function more 670 00:27:06,000 --> 00:27:08,400 powerful but we might still want to 671 00:27:08,400 --> 00:27:10,720 validate the received values in the 672 00:27:10,720 --> 00:27:13,679 following way so say that you never want 673 00:27:13,679 --> 00:27:16,240 to receive a negative number of quantity 674 00:27:16,240 --> 00:27:18,480 and you never want to receive a negative 675 00:27:18,480 --> 00:27:20,880 number of price so that is something 676 00:27:20,880 --> 00:27:23,200 that you cannot achieve by the typings 677 00:27:23,200 --> 00:27:25,279 in here but there's actually a great way 678 00:27:25,279 --> 00:27:27,840 to work this around and that will be by 679 00:27:27,840 --> 00:27:32,000 using assert statements now a third is a 680 00:27:32,000 --> 00:27:36,000 statement keyword that is used to check 681 00:27:36,000 --> 00:27:38,960 if there is a match between what is 682 00:27:38,960 --> 00:27:41,679 happening to your expectations so let's 683 00:27:41,679 --> 00:27:43,840 see how we can get work with assert so 684 00:27:43,840 --> 00:27:45,520 i'm actually going to delete this from 685 00:27:45,520 --> 00:27:48,880 here and i'm going to organize our init 686 00:27:48,880 --> 00:27:50,880 method a little bit i'm going to say 687 00:27:50,880 --> 00:27:54,480 here a comment and i will say assign to 688 00:27:54,480 --> 00:27:55,840 self object 689 00:27:55,840 --> 00:27:59,039 and i will say up top something like 690 00:27:59,039 --> 00:28:02,159 run validations to the received 691 00:28:02,159 --> 00:28:03,440 arguments 692 00:28:03,440 --> 00:28:06,880 all right so now it is a great idea to 693 00:28:06,880 --> 00:28:09,840 validate that the price and quantity are 694 00:28:09,840 --> 00:28:12,559 both greater than or equal to zero 695 00:28:12,559 --> 00:28:14,720 because we probably don't want to handle 696 00:28:14,720 --> 00:28:16,799 with those when they are negative 697 00:28:16,799 --> 00:28:19,120 numbers and we want to crash the program 698 00:28:19,120 --> 00:28:21,919 so we could say assert and pay attention 699 00:28:21,919 --> 00:28:24,159 that i use it as a statement not a 700 00:28:24,159 --> 00:28:26,480 built-in function or something like that 701 00:28:26,480 --> 00:28:28,840 and i can say here 702 00:28:28,840 --> 00:28:30,799 price is 703 00:28:30,799 --> 00:28:33,200 greater than or equal to 704 00:28:33,200 --> 00:28:36,799 zero now once i set this then i can also 705 00:28:36,799 --> 00:28:38,320 do the same for 706 00:28:38,320 --> 00:28:41,440 quantity actually so let me do that 707 00:28:41,440 --> 00:28:43,279 quickly 708 00:28:43,279 --> 00:28:44,399 by 709 00:28:44,399 --> 00:28:47,440 this way and then once we have this then 710 00:28:47,440 --> 00:28:48,799 i can actually 711 00:28:48,799 --> 00:28:51,279 go ahead and run our program and you 712 00:28:51,279 --> 00:28:53,039 will see that i will not receive any 713 00:28:53,039 --> 00:28:56,240 arrows but the second that i change this 714 00:28:56,240 --> 00:28:59,440 quantity to negative 1 for example and 715 00:28:59,440 --> 00:29:02,000 this one being negative 3 then i will 716 00:29:02,000 --> 00:29:05,120 have some errors that will say assertion 717 00:29:05,120 --> 00:29:08,640 error now you can see that the fact that 718 00:29:08,640 --> 00:29:10,799 we see here assertion error 719 00:29:10,799 --> 00:29:13,200 is quite a general exception that 720 00:29:13,200 --> 00:29:15,360 doesn't mean anything now what is so 721 00:29:15,360 --> 00:29:17,760 beautiful with assert you can add your 722 00:29:17,760 --> 00:29:20,720 own exception messages right near of it 723 00:29:20,720 --> 00:29:23,120 as a second argument so 724 00:29:23,120 --> 00:29:25,600 let's go up top here and go back to 725 00:29:25,600 --> 00:29:28,240 those two lines so the first argument 726 00:29:28,240 --> 00:29:30,720 that is passed to the statement is the 727 00:29:30,720 --> 00:29:32,960 statement that would like to check but 728 00:29:32,960 --> 00:29:36,000 if we were to say here comma and use a 729 00:29:36,000 --> 00:29:37,760 string to say 730 00:29:37,760 --> 00:29:39,919 actually a formatted string and i can 731 00:29:39,919 --> 00:29:41,520 say 732 00:29:41,520 --> 00:29:42,880 price 733 00:29:42,880 --> 00:29:45,760 and then refer to the value of it 734 00:29:45,760 --> 00:29:49,200 is not greater than 735 00:29:49,679 --> 00:29:51,679 0 like that 736 00:29:51,679 --> 00:29:54,080 i can add an explanation mark here and i 737 00:29:54,080 --> 00:29:56,240 can use the same thing 738 00:29:56,240 --> 00:29:58,799 copy that with a comma and paste this in 739 00:29:58,799 --> 00:30:03,200 here and change this quantity 740 00:30:03,279 --> 00:30:06,080 and then refer to the value of it 741 00:30:06,080 --> 00:30:08,640 and say that it is not equal to i mean 742 00:30:08,640 --> 00:30:11,039 greater than or equal to zero so it 743 00:30:11,039 --> 00:30:12,720 needs to be actually changed to greater 744 00:30:12,720 --> 00:30:15,279 than or equal to 745 00:30:15,279 --> 00:30:16,720 like that 746 00:30:16,720 --> 00:30:19,120 and same goes for 747 00:30:19,120 --> 00:30:20,000 here 748 00:30:20,000 --> 00:30:21,600 and i have some 749 00:30:21,600 --> 00:30:23,679 space here that should be deleted all 750 00:30:23,679 --> 00:30:26,320 right so now if i was to execute our 751 00:30:26,320 --> 00:30:28,559 program then you can see that we receive 752 00:30:28,559 --> 00:30:31,200 assertion error quantity 753 00:30:31,200 --> 00:30:34,159 minus one is not greater or equal than 754 00:30:34,159 --> 00:30:36,559 zero so i should delete this 755 00:30:36,559 --> 00:30:39,360 then here for that and now it is perfect 756 00:30:39,360 --> 00:30:41,200 so now we understand that using the 757 00:30:41,200 --> 00:30:43,360 assert statement could allow us to 758 00:30:43,360 --> 00:30:46,159 validate the arguments that we receive 759 00:30:46,159 --> 00:30:48,480 and also it allows us to catch up the 760 00:30:48,480 --> 00:30:50,480 bugs as soon as possible 761 00:30:50,480 --> 00:30:52,559 before going forward with the rest of 762 00:30:52,559 --> 00:30:55,120 the actions that we like to take within 763 00:30:55,120 --> 00:30:57,200 this program so let me actually change 764 00:30:57,200 --> 00:31:00,880 those back to valid values like that 765 00:31:00,880 --> 00:31:03,519 and that is perfect alright so until 766 00:31:03,519 --> 00:31:05,519 this point we learned about how to work 767 00:31:05,519 --> 00:31:07,679 with the constructor and we also learned 768 00:31:07,679 --> 00:31:09,760 about how to assign different attributes 769 00:31:09,760 --> 00:31:12,080 to instances that are going to be unique 770 00:31:12,080 --> 00:31:14,480 per instance which means that you can go 771 00:31:14,480 --> 00:31:16,320 ahead and create as much as instances as 772 00:31:16,320 --> 00:31:18,159 you want and you have the control to 773 00:31:18,159 --> 00:31:20,320 pass whatever values you'd like to for 774 00:31:20,320 --> 00:31:23,120 the name price and quantity now consider 775 00:31:23,120 --> 00:31:25,279 a situation that you'll want to make use 776 00:31:25,279 --> 00:31:27,600 of an attribute that is going to be 777 00:31:27,600 --> 00:31:31,279 global or across all the instances now a 778 00:31:31,279 --> 00:31:33,360 good candidate for example of this could 779 00:31:33,360 --> 00:31:35,679 be a situation that you'll want to apply 780 00:31:35,679 --> 00:31:38,159 a sale on your shop so this means that 781 00:31:38,159 --> 00:31:40,240 you want to go ahead and having the 782 00:31:40,240 --> 00:31:42,720 control of applying some discount for 783 00:31:42,720 --> 00:31:45,679 each one of the items and that is a good 784 00:31:45,679 --> 00:31:48,159 candidate for creating an attribute that 785 00:31:48,159 --> 00:31:50,480 is going to be shared across all the 786 00:31:50,480 --> 00:31:54,000 instances now we call those kind of 787 00:31:54,000 --> 00:31:56,720 attributes class attributes and the kind 788 00:31:56,720 --> 00:31:58,480 of attribute that we have learned till 789 00:31:58,480 --> 00:32:00,720 this point is actually called in a full 790 00:32:00,720 --> 00:32:03,440 name instance attributes so about 791 00:32:03,440 --> 00:32:05,200 instance attributes we know everything 792 00:32:05,200 --> 00:32:07,039 and we learned how to work with it but 793 00:32:07,039 --> 00:32:09,120 we did not work it with the other kind 794 00:32:09,120 --> 00:32:11,039 of the attributes which we will do in 795 00:32:11,039 --> 00:32:13,360 this tutorial which is called again a 796 00:32:13,360 --> 00:32:16,240 class attribute so a class attribute is 797 00:32:16,240 --> 00:32:18,080 an attribute that is going to be belong 798 00:32:18,080 --> 00:32:21,200 to the class itself but however you can 799 00:32:21,200 --> 00:32:23,679 also access this attribute from the 800 00:32:23,679 --> 00:32:26,000 instance level as well let's go ahead 801 00:32:26,000 --> 00:32:28,159 and see a good candidate for a class 802 00:32:28,159 --> 00:32:29,840 attribute that you want to go ahead and 803 00:32:29,840 --> 00:32:32,480 create it so that's gonna be going to 804 00:32:32,480 --> 00:32:35,519 our class here and just in the first 805 00:32:35,519 --> 00:32:37,519 line inside our class i can go ahead and 806 00:32:37,519 --> 00:32:39,600 create a class attribute so let's go 807 00:32:39,600 --> 00:32:41,440 ahead and create an attribute like pay 808 00:32:41,440 --> 00:32:44,559 rate equals to 0.8 and the reason that 809 00:32:44,559 --> 00:32:46,399 i'm doing this is because i said that 810 00:32:46,399 --> 00:32:48,000 there is going to be 20 percent of 811 00:32:48,000 --> 00:32:50,880 discount so i probably want to store an 812 00:32:50,880 --> 00:32:52,960 attribute that will describe how much i 813 00:32:52,960 --> 00:32:54,559 still need to pay 814 00:32:54,559 --> 00:32:58,480 so i will say here the pay rate after 815 00:32:58,480 --> 00:33:02,480 20 percent discount like that okay so 816 00:33:02,480 --> 00:33:04,799 now that we have created this then let's 817 00:33:04,799 --> 00:33:07,120 see what are the ways that we can access 818 00:33:07,120 --> 00:33:09,840 this attribute now if i was to go down 819 00:33:09,840 --> 00:33:12,640 and actually deleting one of those and 820 00:33:12,640 --> 00:33:14,640 say something inside this print line 821 00:33:14,640 --> 00:33:16,240 that will look like the following so i 822 00:33:16,240 --> 00:33:18,159 will try to access to the reference of 823 00:33:18,159 --> 00:33:19,919 the class itself so i'm not going to 824 00:33:19,919 --> 00:33:22,399 create an instance like that besides i'm 825 00:33:22,399 --> 00:33:24,240 just going to bring in the reference to 826 00:33:24,240 --> 00:33:26,640 the class level itself and i'm going to 827 00:33:26,640 --> 00:33:28,880 try to access this attribute by saying 828 00:33:28,880 --> 00:33:31,760 pay underscore rate now if i was to run 829 00:33:31,760 --> 00:33:33,440 that then you're going to see that as 830 00:33:33,440 --> 00:33:35,840 expected we see this class attribute 831 00:33:35,840 --> 00:33:37,679 because that is a way that you can 832 00:33:37,679 --> 00:33:40,240 access those class attributes now this 833 00:33:40,240 --> 00:33:42,480 might be confusing but i said a minute 834 00:33:42,480 --> 00:33:45,039 ago that you can also access those class 835 00:33:45,039 --> 00:33:47,760 attributes from the instance level well 836 00:33:47,760 --> 00:33:49,919 let's see if that is true so if i was to 837 00:33:49,919 --> 00:33:53,200 duplicate those lines twice by using the 838 00:33:53,200 --> 00:33:55,519 shortcut of control d 839 00:33:55,519 --> 00:33:57,519 then let's go ahead and change those to 840 00:33:57,519 --> 00:33:59,200 item one 841 00:33:59,200 --> 00:34:03,440 and this one to item two now see how i 842 00:34:03,440 --> 00:34:06,080 try to access the pay rate attribute 843 00:34:06,080 --> 00:34:08,320 from the instance although we don't have 844 00:34:08,320 --> 00:34:10,879 such an instance attribute now if i was 845 00:34:10,879 --> 00:34:12,719 to run that then you're gonna see that 846 00:34:12,719 --> 00:34:15,918 we still have the access to see that 847 00:34:15,918 --> 00:34:18,079 class attribute well that might be 848 00:34:18,079 --> 00:34:20,159 confusing and that might be hard to 849 00:34:20,159 --> 00:34:22,399 understand why that is happening well 850 00:34:22,399 --> 00:34:23,918 there is actually something that we need 851 00:34:23,918 --> 00:34:25,679 to understand when we work with 852 00:34:25,679 --> 00:34:28,000 instances in python so when we have an 853 00:34:28,000 --> 00:34:30,800 instance on our hand then at first this 854 00:34:30,800 --> 00:34:33,599 instance tries to bring the attribute 855 00:34:33,599 --> 00:34:34,800 from the 856 00:34:34,800 --> 00:34:37,839 instance level at first stage but if it 857 00:34:37,839 --> 00:34:40,159 doesn't find it there then it is going 858 00:34:40,159 --> 00:34:42,560 to try to bring that attribute from the 859 00:34:42,560 --> 00:34:45,119 class level so what that means it means 860 00:34:45,119 --> 00:34:48,079 that item one did some thinking here and 861 00:34:48,079 --> 00:34:50,320 say to itself okay so i don't have this 862 00:34:50,320 --> 00:34:52,719 attribute right in here because that is 863 00:34:52,719 --> 00:34:54,639 just not an attribute that assigned to 864 00:34:54,639 --> 00:34:56,639 me so i'm going to try to search that 865 00:34:56,639 --> 00:34:58,640 from the instance level and then i'm 866 00:34:58,640 --> 00:35:01,040 going to find it and print it back so 867 00:35:01,040 --> 00:35:03,200 that is exactly what is happening here 868 00:35:03,200 --> 00:35:06,400 item 1 and item 2 are instances that 869 00:35:06,400 --> 00:35:09,280 could not find the pay rate attribute on 870 00:35:09,280 --> 00:35:12,079 the instance level so both of them went 871 00:35:12,079 --> 00:35:14,640 ahead and tried to bring this attribute 872 00:35:14,640 --> 00:35:17,119 from the class level and since it really 873 00:35:17,119 --> 00:35:18,880 exists in the class level then we were 874 00:35:18,880 --> 00:35:20,880 able to access those now to even give 875 00:35:20,880 --> 00:35:23,040 you a better idea of what is going on 876 00:35:23,040 --> 00:35:24,960 here then i'm going to do one more 877 00:35:24,960 --> 00:35:27,119 additional thing now i will delete this 878 00:35:27,119 --> 00:35:29,599 first print line and i will go ahead and 879 00:35:29,599 --> 00:35:31,760 delete those attributes from here as 880 00:35:31,760 --> 00:35:35,040 well now there is a built-in magic 881 00:35:35,040 --> 00:35:37,599 attribute not a magic method that you 882 00:35:37,599 --> 00:35:39,599 can go ahead and see all the attributes 883 00:35:39,599 --> 00:35:41,920 that are belonging to that specific 884 00:35:41,920 --> 00:35:45,280 object and that is achievable by using 885 00:35:45,280 --> 00:35:49,040 this double underscore dict double 886 00:35:49,040 --> 00:35:52,160 underscore like that so this will go 887 00:35:52,160 --> 00:35:53,839 ahead and try to bring you all the 888 00:35:53,839 --> 00:35:56,000 attributes that are belonging to the 889 00:35:56,000 --> 00:35:57,920 object that you apply this attribute and 890 00:35:57,920 --> 00:36:00,160 want to see its content so i will go 891 00:36:00,160 --> 00:36:01,280 ahead and 892 00:36:01,280 --> 00:36:03,280 copy this one and paste this in for the 893 00:36:03,280 --> 00:36:05,760 instance level as well so this should 894 00:36:05,760 --> 00:36:08,800 give me all the attributes for class 895 00:36:08,800 --> 00:36:09,760 level 896 00:36:09,760 --> 00:36:11,760 and the second line 897 00:36:11,760 --> 00:36:14,079 should do this for the 898 00:36:14,079 --> 00:36:16,320 instance level all right and if i was to 899 00:36:16,320 --> 00:36:19,040 run that then let's explore the results 900 00:36:19,040 --> 00:36:21,520 for a second now you can see that at the 901 00:36:21,520 --> 00:36:24,160 first line we see this pay rate 902 00:36:24,160 --> 00:36:26,560 attribute but in the second line we 903 00:36:26,560 --> 00:36:28,720 never see it we see name price and 904 00:36:28,720 --> 00:36:31,200 quantity and you can also pay attention 905 00:36:31,200 --> 00:36:33,119 that this magic attribute is actually 906 00:36:33,119 --> 00:36:35,359 responsible to take all the attributes 907 00:36:35,359 --> 00:36:37,680 and convert this to a dictionary and 908 00:36:37,680 --> 00:36:39,520 that is from where the 909 00:36:39,520 --> 00:36:41,119 keyword coming from it is just a 910 00:36:41,119 --> 00:36:43,680 shortened version of a dictionary so 911 00:36:43,680 --> 00:36:45,680 that is a very useful magic attribute 912 00:36:45,680 --> 00:36:47,520 that you can go ahead and use if you 913 00:36:47,520 --> 00:36:49,359 just want to see temporarily for 914 00:36:49,359 --> 00:36:51,680 debugging reasons all the attributes 915 00:36:51,680 --> 00:36:54,079 that are belonging to some object all 916 00:36:54,079 --> 00:36:56,079 right so now that we understood this 917 00:36:56,079 --> 00:36:57,760 then let's take it to a real life 918 00:36:57,760 --> 00:36:59,599 example and come up with a method that 919 00:36:59,599 --> 00:37:02,000 will go ahead and apply a discount on 920 00:37:02,000 --> 00:37:04,640 our items price so that will be by 921 00:37:04,640 --> 00:37:06,880 creating a method that will be belong to 922 00:37:06,880 --> 00:37:09,040 each of our instances and that means 923 00:37:09,040 --> 00:37:10,880 that we can go ahead and come up with a 924 00:37:10,880 --> 00:37:13,839 method that we could name apply discount 925 00:37:13,839 --> 00:37:15,839 so let's go ahead and start working on 926 00:37:15,839 --> 00:37:18,400 this so i'm going to say def apply 927 00:37:18,400 --> 00:37:20,320 discount and pay attention that i'm 928 00:37:20,320 --> 00:37:23,040 using a new method inside a class here 929 00:37:23,040 --> 00:37:23,760 so 930 00:37:23,760 --> 00:37:26,800 right inside of this then at first we 931 00:37:26,800 --> 00:37:29,040 need to figure out how we are going to 932 00:37:29,040 --> 00:37:31,280 override an attribute that is belonging 933 00:37:31,280 --> 00:37:33,520 to an instance and we already know that 934 00:37:33,520 --> 00:37:35,680 we can do that with the self keyword so 935 00:37:35,680 --> 00:37:37,599 it will be self 936 00:37:37,599 --> 00:37:39,040 dot price 937 00:37:39,040 --> 00:37:41,599 and that will be equal to self.price 938 00:37:41,599 --> 00:37:43,920 meaning the older value of this 939 00:37:43,920 --> 00:37:45,280 attribute 940 00:37:45,280 --> 00:37:49,839 multiplied by the pay rate now you might 941 00:37:49,839 --> 00:37:51,520 expect that we could access this 942 00:37:51,520 --> 00:37:53,920 directly like that but if you remember 943 00:37:53,920 --> 00:37:56,000 that is actually belonging to the item 944 00:37:56,000 --> 00:37:58,480 class itself now this might be confusing 945 00:37:58,480 --> 00:38:01,040 because this method already inside this 946 00:38:01,040 --> 00:38:03,200 class so you might think already that 947 00:38:03,200 --> 00:38:05,359 you can access it directly by saying pay 948 00:38:05,359 --> 00:38:07,359 rate because it is already inside the 949 00:38:07,359 --> 00:38:09,359 class but that is actually not going to 950 00:38:09,359 --> 00:38:11,520 work because you can either access it 951 00:38:11,520 --> 00:38:13,520 from the class level or the instance 952 00:38:13,520 --> 00:38:15,599 level as we understood previously so i 953 00:38:15,599 --> 00:38:18,480 can go ahead and say item dot pay rate 954 00:38:18,480 --> 00:38:20,480 like that and there you have a method 955 00:38:20,480 --> 00:38:22,960 that can go ahead and basically override 956 00:38:22,960 --> 00:38:25,520 the price attribute for one of your 957 00:38:25,520 --> 00:38:28,720 items now to show you that this works 958 00:38:28,720 --> 00:38:31,920 then i can only use one instance for now 959 00:38:31,920 --> 00:38:34,480 and i can go ahead and call this method 960 00:38:34,480 --> 00:38:38,640 by say apply discount and i can also 961 00:38:38,640 --> 00:38:41,680 now try to print the attribute of price 962 00:38:41,680 --> 00:38:43,280 for this item 963 00:38:43,280 --> 00:38:46,480 one and we should see 80 right so if we 964 00:38:46,480 --> 00:38:48,560 were to run that then we're going to see 965 00:38:48,560 --> 00:38:51,040 that we are going to receive 80.0 as 966 00:38:51,040 --> 00:38:53,280 expected now we should not forget the 967 00:38:53,280 --> 00:38:55,200 option that you might also want to have 968 00:38:55,200 --> 00:38:57,599 a different discount amount for a 969 00:38:57,599 --> 00:39:00,240 specific item so say that one day you'll 970 00:39:00,240 --> 00:39:03,520 have 20 items and only for the laptop 971 00:39:03,520 --> 00:39:06,640 you want to have a 30 discount but it is 972 00:39:06,640 --> 00:39:08,960 going to be a bad idea changing the 973 00:39:08,960 --> 00:39:12,000 class attribute to 0.7 because it will 974 00:39:12,000 --> 00:39:14,079 affect all the items that you have right 975 00:39:14,079 --> 00:39:16,079 now on your hand so what you can do 976 00:39:16,079 --> 00:39:18,800 instead is you can assign this attribute 977 00:39:18,800 --> 00:39:21,280 directly to one of the instances that 978 00:39:21,280 --> 00:39:22,720 you would like to have a different 979 00:39:22,720 --> 00:39:25,119 discount amount for so let's go ahead 980 00:39:25,119 --> 00:39:27,040 and see an example for this so i will 981 00:39:27,040 --> 00:39:29,440 allow myself to bring back the item of 982 00:39:29,440 --> 00:39:33,200 laptop and then what i can do to apply a 983 00:39:33,200 --> 00:39:36,880 30 discount for this item is assigning 984 00:39:36,880 --> 00:39:39,680 the exact same attribute to the instance 985 00:39:39,680 --> 00:39:41,839 so i can go ahead and use a item tool 986 00:39:41,839 --> 00:39:43,839 dot pay on the score rate 987 00:39:43,839 --> 00:39:45,320 is equal to 988 00:39:45,320 --> 00:39:48,880 0.7 now what will happen here is that 989 00:39:48,880 --> 00:39:51,920 for item 2 it will find the attribute of 990 00:39:51,920 --> 00:39:54,640 pay rate in the instance level so item 2 991 00:39:54,640 --> 00:39:56,240 does not really have to go ahead to the 992 00:39:56,240 --> 00:39:58,800 class level and bring back the value of 993 00:39:58,800 --> 00:40:01,119 pay rate because at first look it is 994 00:40:01,119 --> 00:40:03,520 going to find it in the instance level 995 00:40:03,520 --> 00:40:05,520 but for item 1 it is different it is 996 00:40:05,520 --> 00:40:08,079 still going to read from the item level 997 00:40:08,079 --> 00:40:10,480 which is going to be 0.8 998 00:40:10,480 --> 00:40:14,480 so now if we were to try to use item two 999 00:40:14,480 --> 00:40:17,680 dot apply discount and as well as 1000 00:40:17,680 --> 00:40:21,520 printing the price now then let's see 1001 00:40:21,520 --> 00:40:24,640 what will happen so i will uncomment 1002 00:40:24,640 --> 00:40:27,680 this line to not see this print for now 1003 00:40:27,680 --> 00:40:29,359 and i will go ahead and execute our 1004 00:40:29,359 --> 00:40:32,960 program now you can see that we still 1005 00:40:32,960 --> 00:40:35,760 however receive 800 1006 00:40:35,760 --> 00:40:38,000 and what this means this means that the 1007 00:40:38,000 --> 00:40:40,400 discount that has been applied is still 1008 00:40:40,400 --> 00:40:41,839 20 1009 00:40:41,839 --> 00:40:44,319 and where this is coming from well this 1010 00:40:44,319 --> 00:40:47,680 is coming from this method here that no 1011 00:40:47,680 --> 00:40:50,400 matter what we try to pull the pay rate 1012 00:40:50,400 --> 00:40:52,960 from the class level so a best practice 1013 00:40:52,960 --> 00:40:56,720 here will be to change this to self and 1014 00:40:56,720 --> 00:40:59,440 that way if we overwrite the pay rate 1015 00:40:59,440 --> 00:41:01,680 for the instance level then it is going 1016 00:41:01,680 --> 00:41:04,640 to read from the instance level but for 1017 00:41:04,640 --> 00:41:08,160 item one if we try to access the pay 1018 00:41:08,160 --> 00:41:10,960 rate from the instance level then this 1019 00:41:10,960 --> 00:41:13,599 is still great because we did not assign 1020 00:41:13,599 --> 00:41:16,079 a specific pay rate for item one so it 1021 00:41:16,079 --> 00:41:17,680 is going to pull that from the class 1022 00:41:17,680 --> 00:41:20,880 level now if we were to try to run that 1023 00:41:20,880 --> 00:41:22,560 then you're gonna see now that we have 1024 00:41:22,560 --> 00:41:25,599 expected results and if we were to also 1025 00:41:25,599 --> 00:41:28,079 uncomment the first print line for the 1026 00:41:28,079 --> 00:41:31,440 item one and rerun our program then you 1027 00:41:31,440 --> 00:41:34,160 can see that for item one we had twenty 1028 00:41:34,160 --> 00:41:37,040 percent discount and for item two we had 1029 00:41:37,040 --> 00:41:39,200 thirty percent discount so when it comes 1030 00:41:39,200 --> 00:41:41,920 to accessing class attributes you might 1031 00:41:41,920 --> 00:41:44,319 want to reconsider how you want to 1032 00:41:44,319 --> 00:41:46,079 access them when you come up with some 1033 00:41:46,079 --> 00:41:48,800 methods and specifically for creating a 1034 00:41:48,800 --> 00:41:51,359 method like apply discount it is a great 1035 00:41:51,359 --> 00:41:53,200 idea to access it from the instance 1036 00:41:53,200 --> 00:41:55,760 level so you also allow the option of 1037 00:41:55,760 --> 00:41:58,800 using a pay rate that is assigned to the 1038 00:41:58,800 --> 00:42:00,880 instance level okay so now that we 1039 00:42:00,880 --> 00:42:02,400 understood completely about the 1040 00:42:02,400 --> 00:42:04,079 differences between a class two and 1041 00:42:04,079 --> 00:42:06,079 instance attribute let's jump ahead to 1042 00:42:06,079 --> 00:42:08,480 the next topic now you see that i have 1043 00:42:08,480 --> 00:42:10,400 deleted those print lines that i have 1044 00:42:10,400 --> 00:42:12,640 down below and i came up with five 1045 00:42:12,640 --> 00:42:14,880 instances that i have created here so 1046 00:42:14,880 --> 00:42:16,880 you might also want to create those five 1047 00:42:16,880 --> 00:42:19,520 instances immediately so that is why i 1048 00:42:19,520 --> 00:42:21,440 will recommend you to go ahead to my 1049 00:42:21,440 --> 00:42:23,839 repository accessing this class 1050 00:42:23,839 --> 00:42:26,000 attributes directory and then code 1051 00:42:26,000 --> 00:42:27,920 snippets and then 1052 00:42:27,920 --> 00:42:30,400 go ahead and copy the code from these 1053 00:42:30,400 --> 00:42:33,359 five underscore items.py file okay so 1054 00:42:33,359 --> 00:42:35,520 considering a situation that your shop 1055 00:42:35,520 --> 00:42:37,359 is going to be larger in the future 1056 00:42:37,359 --> 00:42:39,040 meaning that you are going to have more 1057 00:42:39,040 --> 00:42:41,280 items then the more items that you are 1058 00:42:41,280 --> 00:42:43,680 going to have the more filtration like 1059 00:42:43,680 --> 00:42:46,079 things that you want to do in the future 1060 00:42:46,079 --> 00:42:47,920 but what is problematic currently with 1061 00:42:47,920 --> 00:42:50,240 our class is the fact that we don't have 1062 00:42:50,240 --> 00:42:52,480 any resource where we can just access 1063 00:42:52,480 --> 00:42:54,400 all the items that we have in our shop 1064 00:42:54,400 --> 00:42:56,960 right now now it could have been nicer 1065 00:42:56,960 --> 00:42:58,480 if we could somehow 1066 00:42:58,480 --> 00:43:01,200 have a list with all the item instances 1067 00:43:01,200 --> 00:43:03,520 that have been created until this point 1068 00:43:03,520 --> 00:43:05,920 but currently there is not an approach 1069 00:43:05,920 --> 00:43:08,480 that will give us a list with five 1070 00:43:08,480 --> 00:43:10,880 elements where each element will 1071 00:43:10,880 --> 00:43:13,760 represent an instance of a class so in 1072 00:43:13,760 --> 00:43:16,560 order to come up with such a design then 1073 00:43:16,560 --> 00:43:18,720 here is a wonderful candidate for 1074 00:43:18,720 --> 00:43:21,280 creating a class attribute that we could 1075 00:43:21,280 --> 00:43:24,560 name all and once we do this then we're 1076 00:43:24,560 --> 00:43:26,560 gonna see how we are going to add our 1077 00:43:26,560 --> 00:43:29,359 instances to that list so i will go 1078 00:43:29,359 --> 00:43:34,240 ahead and start by going here and use an 1079 00:43:34,240 --> 00:43:36,480 old attribute so it will be 1080 00:43:36,480 --> 00:43:37,760 all 1081 00:43:37,760 --> 00:43:41,200 equals to an empty list now we need to 1082 00:43:41,200 --> 00:43:44,240 figure out how we are going to add our 1083 00:43:44,240 --> 00:43:46,640 instances for each time that we are 1084 00:43:46,640 --> 00:43:48,880 going to go ahead and create an instance 1085 00:43:48,880 --> 00:43:51,119 now if you remember the double 1086 00:43:51,119 --> 00:43:54,160 underscore init method is being called 1087 00:43:54,160 --> 00:43:56,800 immediately once the instance has been 1088 00:43:56,800 --> 00:44:00,319 created so it might be a wonderful idea 1089 00:44:00,319 --> 00:44:02,400 going down below inside this double 1090 00:44:02,400 --> 00:44:05,520 underscore init method and use a code 1091 00:44:05,520 --> 00:44:07,760 that will be responsible to append to 1092 00:44:07,760 --> 00:44:10,160 that list every time that we create an 1093 00:44:10,160 --> 00:44:12,640 instance and that will be as easy as 1094 00:44:12,640 --> 00:44:15,040 saying something like the following so 1095 00:44:15,040 --> 00:44:16,880 first you could pay attention that i 1096 00:44:16,880 --> 00:44:18,880 actually wrote some commands in this 1097 00:44:18,880 --> 00:44:21,119 double underscore init function like run 1098 00:44:21,119 --> 00:44:24,160 validations and assign to self object so 1099 00:44:24,160 --> 00:44:25,839 it might be a great idea to start with a 1100 00:44:25,839 --> 00:44:29,040 comment here that will say actions 1101 00:44:29,040 --> 00:44:31,280 to execute just to really have a great 1102 00:44:31,280 --> 00:44:33,040 separation between the different things 1103 00:44:33,040 --> 00:44:35,440 that we are doing so now inside here i 1104 00:44:35,440 --> 00:44:36,960 can say item 1105 00:44:36,960 --> 00:44:39,680 dot all and you can see that i use the 1106 00:44:39,680 --> 00:44:42,560 class object first and then that is a 1107 00:44:42,560 --> 00:44:44,319 list so i can use 1108 00:44:44,319 --> 00:44:47,839 dot append and then i will just append 1109 00:44:47,839 --> 00:44:51,200 the self object now we know that self is 1110 00:44:51,200 --> 00:44:53,359 actually the instance itself every time 1111 00:44:53,359 --> 00:44:56,800 that it is being created so once we go 1112 00:44:56,800 --> 00:44:59,359 ahead and launch such a command inside 1113 00:44:59,359 --> 00:45:02,720 the init function then for each instance 1114 00:45:02,720 --> 00:45:05,359 that is going to be created this whole 1115 00:45:05,359 --> 00:45:07,599 list is going to be filled with our 1116 00:45:07,599 --> 00:45:10,240 instances now to show you that i can 1117 00:45:10,240 --> 00:45:12,960 jump a line after we create the 1118 00:45:12,960 --> 00:45:16,079 instances and we can say print 1119 00:45:16,079 --> 00:45:21,280 item dot all and now if i was to run our 1120 00:45:21,280 --> 00:45:23,119 program then you are going to see that 1121 00:45:23,119 --> 00:45:25,040 we are going to have a list with five 1122 00:45:25,040 --> 00:45:28,160 instances if i was to scroll right a bit 1123 00:45:28,160 --> 00:45:30,000 then you can see that i have exactly 1124 00:45:30,000 --> 00:45:33,200 five elements and that is perfect now 1125 00:45:33,200 --> 00:45:35,280 that's going to be extremely useful if 1126 00:45:35,280 --> 00:45:37,200 you want to do something with only one 1127 00:45:37,200 --> 00:45:39,920 of the attributes of your instances so 1128 00:45:39,920 --> 00:45:41,839 say that you would like to print all the 1129 00:45:41,839 --> 00:45:44,720 names for all of your instances then you 1130 00:45:44,720 --> 00:45:47,040 can use easily a for loop to achieve 1131 00:45:47,040 --> 00:45:49,599 such a task so we can go ahead and say 1132 00:45:49,599 --> 00:45:50,480 for 1133 00:45:50,480 --> 00:45:51,599 instance 1134 00:45:51,599 --> 00:45:52,480 in 1135 00:45:52,480 --> 00:45:56,079 item dot all and you can say print 1136 00:45:56,079 --> 00:45:59,200 instance dot name and once we come up 1137 00:45:59,200 --> 00:46:01,200 with this then you can see that we have 1138 00:46:01,200 --> 00:46:03,200 all the names for all the instances that 1139 00:46:03,200 --> 00:46:05,280 we have created so that is going to be 1140 00:46:05,280 --> 00:46:07,599 useful here and there especially if you 1141 00:46:07,599 --> 00:46:09,680 know how to use the filter function for 1142 00:46:09,680 --> 00:46:12,560 example to apply some special things on 1143 00:46:12,560 --> 00:46:14,480 some of the instances that are matching 1144 00:46:14,480 --> 00:46:16,960 your criteria all right so now that we 1145 00:46:16,960 --> 00:46:19,520 understood this then let's also take 1146 00:46:19,520 --> 00:46:21,520 care of one problem that we saw 1147 00:46:21,520 --> 00:46:24,880 previously now if i was to use a ctrl z 1148 00:46:24,880 --> 00:46:26,160 a couple of times 1149 00:46:26,160 --> 00:46:29,200 and still use this print item.all 1150 00:46:29,200 --> 00:46:32,480 now you could see that the way that the 1151 00:46:32,480 --> 00:46:35,599 object is being represented is not too 1152 00:46:35,599 --> 00:46:37,920 friendly now it could have been nicer if 1153 00:46:37,920 --> 00:46:40,560 we could somehow change the way that the 1154 00:46:40,560 --> 00:46:44,160 object is being represented in this list 1155 00:46:44,160 --> 00:46:46,240 here now there's actually a way to 1156 00:46:46,240 --> 00:46:49,119 achieve this by using a magic method 1157 00:46:49,119 --> 00:46:51,760 inside our class now there is a magic 1158 00:46:51,760 --> 00:46:53,680 method that is called 1159 00:46:53,680 --> 00:46:57,680 double underscore repr and repr stands 1160 00:46:57,680 --> 00:47:00,880 for representing your objects so that is 1161 00:47:00,880 --> 00:47:03,520 why you can actually go ahead and use 1162 00:47:03,520 --> 00:47:05,760 this magic method and then you will have 1163 00:47:05,760 --> 00:47:08,960 the control to display your objects when 1164 00:47:08,960 --> 00:47:11,440 you are printing them in the console now 1165 00:47:11,440 --> 00:47:13,599 i actually recommend watching a video 1166 00:47:13,599 --> 00:47:16,000 that compares between a method that is 1167 00:47:16,000 --> 00:47:17,920 similar to it which is called double 1168 00:47:17,920 --> 00:47:20,640 underscore str and you can take a look 1169 00:47:20,640 --> 00:47:22,960 in the description of this entire series 1170 00:47:22,960 --> 00:47:24,960 to actually watch the video that i'm 1171 00:47:24,960 --> 00:47:26,960 talking about all right so let's go 1172 00:47:26,960 --> 00:47:29,280 ahead and use the repair method to 1173 00:47:29,280 --> 00:47:31,119 understand how this is going to work so 1174 00:47:31,119 --> 00:47:33,760 i'm going to say def inside our class 1175 00:47:33,760 --> 00:47:36,480 and i'm going to use double underscore 1176 00:47:36,480 --> 00:47:40,319 re pr double underscore and as expected 1177 00:47:40,319 --> 00:47:43,440 it will receive the self now what we can 1178 00:47:43,440 --> 00:47:46,640 do now is returning a string that will 1179 00:47:46,640 --> 00:47:49,440 be responsible to represent this object 1180 00:47:49,440 --> 00:47:51,839 now obviously we don't want to use 1181 00:47:51,839 --> 00:47:54,559 something that is not unique for each of 1182 00:47:54,559 --> 00:47:57,040 the instances because say that i was to 1183 00:47:57,040 --> 00:48:00,000 use now return item something like that 1184 00:48:00,000 --> 00:48:01,119 and 1185 00:48:01,119 --> 00:48:03,200 run our program then you can see that 1186 00:48:03,200 --> 00:48:05,599 i'm going to receive a list with this 1187 00:48:05,599 --> 00:48:08,079 string five times but it is going to be 1188 00:48:08,079 --> 00:48:09,760 hard to identify 1189 00:48:09,760 --> 00:48:10,640 which 1190 00:48:10,640 --> 00:48:14,079 instance represents each string here so 1191 00:48:14,079 --> 00:48:16,240 it could be helpful if we were to return 1192 00:48:16,240 --> 00:48:18,800 a string that could be unique so i'm 1193 00:48:18,800 --> 00:48:21,839 going to close the console here and go 1194 00:48:21,839 --> 00:48:24,240 ahead here and use a formatted string 1195 00:48:24,240 --> 00:48:26,480 and in order to make this unique it is a 1196 00:48:26,480 --> 00:48:29,440 best practice to represent it exactly 1197 00:48:29,440 --> 00:48:32,319 like we create the instance 1198 00:48:32,319 --> 00:48:35,040 like that so what i'm going to do here 1199 00:48:35,040 --> 00:48:38,240 is leaving the item and use a 1200 00:48:38,240 --> 00:48:41,440 brackets opener and the closure and then 1201 00:48:41,440 --> 00:48:44,160 i'm going to make the return string here 1202 00:48:44,160 --> 00:48:47,040 as much as equal as possible to the way 1203 00:48:47,040 --> 00:48:48,400 that we create 1204 00:48:48,400 --> 00:48:52,079 those instances so i will start by 1205 00:48:52,079 --> 00:48:55,119 typing here single quotes to escape from 1206 00:48:55,119 --> 00:48:57,119 the double quotes that are coming from 1207 00:48:57,119 --> 00:49:00,240 here and i'm going to refer to the value 1208 00:49:00,240 --> 00:49:04,800 of name by using self.name and then i 1209 00:49:04,800 --> 00:49:07,839 will leave my single quotes and i will 1210 00:49:07,839 --> 00:49:09,040 use a 1211 00:49:09,040 --> 00:49:11,680 comma like that and then i will go ahead 1212 00:49:11,680 --> 00:49:15,040 and refer to the value of our price i 1213 00:49:15,040 --> 00:49:18,800 will use one more comma and i will say 1214 00:49:18,800 --> 00:49:20,720 self.quantity 1215 00:49:20,720 --> 00:49:24,240 now if we were to execute our program 1216 00:49:24,240 --> 00:49:27,359 again then you can see that now we 1217 00:49:27,359 --> 00:49:30,160 receive a list that is way more friendly 1218 00:49:30,160 --> 00:49:32,640 than what we have seen previously and 1219 00:49:32,640 --> 00:49:35,359 you can also see that this 1220 00:49:35,359 --> 00:49:38,000 first element for example is quite 1221 00:49:38,000 --> 00:49:41,040 equivalent to this line here now you 1222 00:49:41,040 --> 00:49:44,240 might be curious why i worked so hard to 1223 00:49:44,240 --> 00:49:47,359 return the representative version of our 1224 00:49:47,359 --> 00:49:50,240 objects the same way that we create them 1225 00:49:50,240 --> 00:49:51,839 so that is just a best practice 1226 00:49:51,839 --> 00:49:53,839 according to python's documentations 1227 00:49:53,839 --> 00:49:55,760 because it will help us to create 1228 00:49:55,760 --> 00:49:58,559 instances immediately by only the effort 1229 00:49:58,559 --> 00:50:01,040 of copying and pasting this to a python 1230 00:50:01,040 --> 00:50:03,119 console so if you think about it right 1231 00:50:03,119 --> 00:50:06,079 now if you open a python console and you 1232 00:50:06,079 --> 00:50:08,640 import this class then it will be as 1233 00:50:08,640 --> 00:50:10,880 easy as grabbing this 1234 00:50:10,880 --> 00:50:13,520 and pasting to the python console and 1235 00:50:13,520 --> 00:50:15,040 then you will have an instance being 1236 00:50:15,040 --> 00:50:17,359 created so that is the single reason 1237 00:50:17,359 --> 00:50:19,599 that i have came up with this approach 1238 00:50:19,599 --> 00:50:21,520 and also for sure i just wanted to 1239 00:50:21,520 --> 00:50:24,319 return a unique string that will really 1240 00:50:24,319 --> 00:50:26,880 represent our instance and you can see 1241 00:50:26,880 --> 00:50:29,520 that it is very easy to identify the 1242 00:50:29,520 --> 00:50:32,960 instances of our class with this list 1243 00:50:32,960 --> 00:50:35,200 and with this approach all right so 1244 00:50:35,200 --> 00:50:37,119 until this point we understood how we 1245 00:50:37,119 --> 00:50:39,680 can change the way that we represent our 1246 00:50:39,680 --> 00:50:42,000 objects and we also understood how we 1247 00:50:42,000 --> 00:50:45,359 can access to all of our instances by 1248 00:50:45,359 --> 00:50:47,599 this class attribute that we 1249 00:50:47,599 --> 00:50:50,800 intentionally named all now in this part 1250 00:50:50,800 --> 00:50:52,720 we are going to take a look to solve one 1251 00:50:52,720 --> 00:50:55,119 more problem that we have in terms of 1252 00:50:55,119 --> 00:50:56,880 best practices when we are going to 1253 00:50:56,880 --> 00:50:58,640 extend this application and add more 1254 00:50:58,640 --> 00:51:01,200 features now you can see that until this 1255 00:51:01,200 --> 00:51:05,520 point we maintain our data as code in 1256 00:51:05,520 --> 00:51:09,359 this main.py file by only instantiating 1257 00:51:09,359 --> 00:51:11,440 those five items now when we will look 1258 00:51:11,440 --> 00:51:13,359 to extend this application and add some 1259 00:51:13,359 --> 00:51:15,440 more features then we might have a 1260 00:51:15,440 --> 00:51:17,680 harder life to add those features 1261 00:51:17,680 --> 00:51:21,920 because the actual data and the code are 1262 00:51:21,920 --> 00:51:24,240 maintained in the same location meaning 1263 00:51:24,240 --> 00:51:27,119 in the same main.py file now you could 1264 00:51:27,119 --> 00:51:29,119 have think about creating a database 1265 00:51:29,119 --> 00:51:31,760 that will maintain this information but 1266 00:51:31,760 --> 00:51:33,680 i want to keep things more simple for 1267 00:51:33,680 --> 00:51:35,599 the purposes of this tutorial and that 1268 00:51:35,599 --> 00:51:37,200 is why i'm going to use something that 1269 00:51:37,200 --> 00:51:40,079 is called csv that you might have heard 1270 00:51:40,079 --> 00:51:40,800 of 1271 00:51:40,800 --> 00:51:44,000 csv stands for comma separated values so 1272 00:51:44,000 --> 00:51:45,920 this means that you could go ahead and 1273 00:51:45,920 --> 00:51:49,599 use a csv file and you could store your 1274 00:51:49,599 --> 00:51:52,559 values as comma separated where each 1275 00:51:52,559 --> 00:51:55,920 line will represent a single structured 1276 00:51:55,920 --> 00:51:58,559 data and csv is a great option here 1277 00:51:58,559 --> 00:52:01,200 because it allows the data to be saved 1278 00:52:01,200 --> 00:52:03,920 in a table structured format all right 1279 00:52:03,920 --> 00:52:06,800 so let's go ahead and create a csv file 1280 00:52:06,800 --> 00:52:08,800 and i will actually go ahead and name 1281 00:52:08,800 --> 00:52:10,359 this 1282 00:52:10,359 --> 00:52:13,520 items.csv like that and i will go ahead 1283 00:52:13,520 --> 00:52:15,760 and paste in some 1284 00:52:15,760 --> 00:52:18,880 csv content that will be responsible at 1285 00:52:18,880 --> 00:52:20,720 the end of the day represent the same 1286 00:52:20,720 --> 00:52:23,520 data that we look to have here so you 1287 00:52:23,520 --> 00:52:26,000 can see that at the first line i have 1288 00:52:26,000 --> 00:52:27,920 name price and quantity and you can see 1289 00:52:27,920 --> 00:52:31,119 that those are comma separated so those 1290 00:52:31,119 --> 00:52:32,880 represents the columns that we are going 1291 00:52:32,880 --> 00:52:35,040 to have as the data that we are going to 1292 00:52:35,040 --> 00:52:37,599 maintain and in the second line and 1293 00:52:37,599 --> 00:52:40,240 further we are going to have some data 1294 00:52:40,240 --> 00:52:42,800 that will represent the actual data that 1295 00:52:42,800 --> 00:52:46,319 we look to maintain so if we were to now 1296 00:52:46,319 --> 00:52:48,880 split the panes then we can see that 1297 00:52:48,880 --> 00:52:51,599 those are quite equivalent and now we 1298 00:52:51,599 --> 00:52:54,400 should only look for a way to read this 1299 00:52:54,400 --> 00:52:57,920 csv file and actually instantiate those 1300 00:52:57,920 --> 00:53:00,319 objects now you can see that i have a 1301 00:53:00,319 --> 00:53:02,960 suggestion by pycharm to install a 1302 00:53:02,960 --> 00:53:05,280 plugin that will support csv files so 1303 00:53:05,280 --> 00:53:07,599 i'm going to just click on that and 1304 00:53:07,599 --> 00:53:10,160 install those plugins and you can see 1305 00:53:10,160 --> 00:53:13,040 that i will have a csv reader here and 1306 00:53:13,040 --> 00:53:15,280 we will see if we will be able to see 1307 00:53:15,280 --> 00:53:18,800 this data in a table which will be a lot 1308 00:53:18,800 --> 00:53:20,880 nicer so let's go ahead and install this 1309 00:53:20,880 --> 00:53:22,559 and now you can see that i have some 1310 00:53:22,559 --> 00:53:24,960 more options that i can actually go 1311 00:53:24,960 --> 00:53:27,599 ahead and use from here i know that this 1312 00:53:27,599 --> 00:53:30,240 is quite small but actually you have 1313 00:53:30,240 --> 00:53:32,400 some tabs that you can go ahead and 1314 00:53:32,400 --> 00:53:34,559 click on them and if i was to click on 1315 00:53:34,559 --> 00:53:37,760 table editor and actually 1316 00:53:37,760 --> 00:53:39,200 give this 1317 00:53:39,200 --> 00:53:41,920 file more focus then you can see that i 1318 00:53:41,920 --> 00:53:44,880 actually have the best way to read this 1319 00:53:44,880 --> 00:53:47,920 data now you can see that i have my 1320 00:53:47,920 --> 00:53:51,839 columns you can see that i have my rows 1321 00:53:51,839 --> 00:53:54,079 and that is quite nice now i can really 1322 00:53:54,079 --> 00:53:56,640 go ahead and visualize my data more 1323 00:53:56,640 --> 00:53:59,280 nicer and it is just more common way to 1324 00:53:59,280 --> 00:54:02,400 maintain your data okay so now that we 1325 00:54:02,400 --> 00:54:05,359 understood how csv files are working 1326 00:54:05,359 --> 00:54:07,599 let's go ahead and read our csv files 1327 00:54:07,599 --> 00:54:09,760 and instantiate the instances in a 1328 00:54:09,760 --> 00:54:12,720 generic way so it makes sense to delete 1329 00:54:12,720 --> 00:54:15,599 those five lines and i'm going to use 1330 00:54:15,599 --> 00:54:16,960 those lines 1331 00:54:16,960 --> 00:54:20,160 below the apply discount and use a 1332 00:54:20,160 --> 00:54:23,359 method that i could name instantiate 1333 00:54:23,359 --> 00:54:24,960 from csv 1334 00:54:24,960 --> 00:54:28,319 like that now you can see that this one 1335 00:54:28,319 --> 00:54:30,800 is also going to receive self because if 1336 00:54:30,800 --> 00:54:32,880 you remember i said that in each method 1337 00:54:32,880 --> 00:54:35,440 that we will design we need to receive 1338 00:54:35,440 --> 00:54:38,880 at least one parameter that will be 1339 00:54:38,880 --> 00:54:42,400 passed as the instance itself because 1340 00:54:42,400 --> 00:54:46,079 this is how python oop works now the 1341 00:54:46,079 --> 00:54:48,319 problem is we are not going to have any 1342 00:54:48,319 --> 00:54:51,119 instances on our hand to call this 1343 00:54:51,119 --> 00:54:53,599 method from the instance because this 1344 00:54:53,599 --> 00:54:55,440 method is actually designed for 1345 00:54:55,440 --> 00:54:58,480 instantiating the object itself so this 1346 00:54:58,480 --> 00:55:00,799 means that this method could not be 1347 00:55:00,799 --> 00:55:03,520 called from an instance so the way that 1348 00:55:03,520 --> 00:55:06,400 this is going to be solved is by 1349 00:55:06,400 --> 00:55:09,359 converting this method into a class 1350 00:55:09,359 --> 00:55:12,799 method now a class method is a method 1351 00:55:12,799 --> 00:55:16,319 that could be accessed in the following 1352 00:55:16,319 --> 00:55:18,880 way so i will use this line to delete 1353 00:55:18,880 --> 00:55:21,520 that and it could be accessed from the 1354 00:55:21,520 --> 00:55:22,880 class level 1355 00:55:22,880 --> 00:55:25,920 only so this will look like item 1356 00:55:25,920 --> 00:55:29,119 dot instantiate from csv and then in 1357 00:55:29,119 --> 00:55:32,160 here we will probably pass our csv file 1358 00:55:32,160 --> 00:55:34,559 so this method should take full 1359 00:55:34,559 --> 00:55:37,119 responsibility to instantiate those 1360 00:55:37,119 --> 00:55:39,839 objects for us so now that we understood 1361 00:55:39,839 --> 00:55:42,079 this let's go ahead and see how we can 1362 00:55:42,079 --> 00:55:44,880 create a class method so for sure we 1363 00:55:44,880 --> 00:55:47,520 need to delete the self and i know that 1364 00:55:47,520 --> 00:55:49,520 we have errors but we are going to solve 1365 00:55:49,520 --> 00:55:52,240 each one of those just in a second now 1366 00:55:52,240 --> 00:55:54,319 in order to convert this to a class 1367 00:55:54,319 --> 00:55:57,520 method we need to use a decorator that 1368 00:55:57,520 --> 00:56:00,000 will be responsible to convert this 1369 00:56:00,000 --> 00:56:01,920 method into a class method now 1370 00:56:01,920 --> 00:56:04,799 decorators in python is just a quick way 1371 00:56:04,799 --> 00:56:06,960 to change the behavior of the functions 1372 00:56:06,960 --> 00:56:09,520 that we will write by basically calling 1373 00:56:09,520 --> 00:56:12,160 them just before the line that we create 1374 00:56:12,160 --> 00:56:15,119 our function so we could use the add 1375 00:56:15,119 --> 00:56:18,160 sign and use the class method 1376 00:56:18,160 --> 00:56:20,880 in here and then this instant sheet from 1377 00:56:20,880 --> 00:56:23,680 csv method will be a class method all 1378 00:56:23,680 --> 00:56:25,760 right so now that we understood this 1379 00:56:25,760 --> 00:56:27,680 then we should also understand one more 1380 00:56:27,680 --> 00:56:30,079 piece of information before we go ahead 1381 00:56:30,079 --> 00:56:32,400 and design this method now i want to 1382 00:56:32,400 --> 00:56:34,960 show you what will happen if i was to 1383 00:56:34,960 --> 00:56:37,440 delete the entire name and try to 1384 00:56:37,440 --> 00:56:39,920 recreate this function here and i will 1385 00:56:39,920 --> 00:56:42,160 just say instantiate 1386 00:56:42,160 --> 00:56:45,040 from csv again now pay attention what 1387 00:56:45,040 --> 00:56:46,640 will happen if i was to open up and 1388 00:56:46,640 --> 00:56:48,720 close the parentheses now you can see 1389 00:56:48,720 --> 00:56:51,520 that it still receives a parameter but 1390 00:56:51,520 --> 00:56:55,119 this time it is named cls 1391 00:56:55,119 --> 00:56:57,440 now what is going on here and the thing 1392 00:56:57,440 --> 00:57:00,160 that is going on here is the fact that 1393 00:57:00,160 --> 00:57:03,119 when we call our class methods then the 1394 00:57:03,119 --> 00:57:04,079 class 1395 00:57:04,079 --> 00:57:06,799 object itself is passed as a first 1396 00:57:06,799 --> 00:57:09,599 argument always in the background so it 1397 00:57:09,599 --> 00:57:12,480 is a bit like the instance where it is 1398 00:57:12,480 --> 00:57:14,880 also passed as the first argument but 1399 00:57:14,880 --> 00:57:17,280 this time when we call a class method in 1400 00:57:17,280 --> 00:57:20,079 this approach then the class reference 1401 00:57:20,079 --> 00:57:22,960 must be passed as a first argument so 1402 00:57:22,960 --> 00:57:26,000 that is why you should still receive at 1403 00:57:26,000 --> 00:57:28,640 least one parameter but we probably 1404 00:57:28,640 --> 00:57:30,640 understand that we could not name this 1405 00:57:30,640 --> 00:57:32,799 self because that is just going to be 1406 00:57:32,799 --> 00:57:35,599 too much confusing okay so now let's go 1407 00:57:35,599 --> 00:57:38,079 ahead and write some code to read the 1408 00:57:38,079 --> 00:57:40,960 csv file and instantiate some objects 1409 00:57:40,960 --> 00:57:43,119 now i'm going to go up top first and i'm 1410 00:57:43,119 --> 00:57:46,720 going to import a library that is called 1411 00:57:46,720 --> 00:57:50,079 csv so i will go here and i will use an 1412 00:57:50,079 --> 00:57:52,880 import csv line because that will be the 1413 00:57:52,880 --> 00:57:54,240 library that will take full 1414 00:57:54,240 --> 00:57:57,200 responsibility to read the csv file and 1415 00:57:57,200 --> 00:57:59,359 then we will see how we can instantiate 1416 00:57:59,359 --> 00:58:01,920 some objects all right so now i can go 1417 00:58:01,920 --> 00:58:05,040 ahead and use a context manager to read 1418 00:58:05,040 --> 00:58:08,079 the items.csv file now both of those 1419 00:58:08,079 --> 00:58:10,240 files are located in the same location 1420 00:58:10,240 --> 00:58:13,040 so i can just directly say wait open 1421 00:58:13,040 --> 00:58:15,200 items dot csv 1422 00:58:15,200 --> 00:58:17,440 and the permission that i will be 1423 00:58:17,440 --> 00:58:19,520 passing here could be r because we only 1424 00:58:19,520 --> 00:58:22,640 look to read this and i will say as 1425 00:58:22,640 --> 00:58:23,680 f 1426 00:58:23,680 --> 00:58:27,680 like that now inside this open i will go 1427 00:58:27,680 --> 00:58:30,799 ahead and use some method to directly 1428 00:58:30,799 --> 00:58:33,200 read this csv which at the end of the 1429 00:58:33,200 --> 00:58:35,680 day will be responsible to convert this 1430 00:58:35,680 --> 00:58:38,640 into a python dictionary so i will say 1431 00:58:38,640 --> 00:58:41,920 reader is equal to csv 1432 00:58:41,920 --> 00:58:46,240 dot dict reader like that and i will 1433 00:58:46,240 --> 00:58:49,839 pass in the content of our file like 1434 00:58:49,839 --> 00:58:52,559 that now this method should go ahead and 1435 00:58:52,559 --> 00:58:55,440 read our content as a list of 1436 00:58:55,440 --> 00:58:57,520 dictionaries but at the end of the day 1437 00:58:57,520 --> 00:58:59,200 we should also go ahead and convert this 1438 00:58:59,200 --> 00:59:00,880 into a list so i will go ahead and 1439 00:59:00,880 --> 00:59:02,559 create one more variable that will be 1440 00:59:02,559 --> 00:59:05,520 equal to items and i will just convert 1441 00:59:05,520 --> 00:59:08,960 the reader into a list and that's it and 1442 00:59:08,960 --> 00:59:10,799 now that we have completed the actions 1443 00:59:10,799 --> 00:59:12,880 that we wanted to complete by reading 1444 00:59:12,880 --> 00:59:16,480 this csv file let's go ahead and use a 1445 00:59:16,480 --> 00:59:19,680 shift tab to indent out and now before 1446 00:59:19,680 --> 00:59:22,000 we go ahead and instantiate some objects 1447 00:59:22,000 --> 00:59:23,680 let's go ahead and see the results of 1448 00:59:23,680 --> 00:59:26,640 iterating over the items list now i will 1449 00:59:26,640 --> 00:59:29,839 go ahead and use for item in 1450 00:59:29,839 --> 00:59:30,880 items 1451 00:59:30,880 --> 00:59:33,599 and then i will just use print items to 1452 00:59:33,599 --> 00:59:36,160 show you the behavior of that and excuse 1453 00:59:36,160 --> 00:59:38,799 me it should be item all right so now 1454 00:59:38,799 --> 00:59:40,799 that we understood this then let's go 1455 00:59:40,799 --> 00:59:42,559 ahead and see what we have in those 1456 00:59:42,559 --> 00:59:45,200 lines so after our class definition we 1457 00:59:45,200 --> 00:59:46,839 only go ahead and call this 1458 00:59:46,839 --> 00:59:50,000 item.instantiate from csv method so if i 1459 00:59:50,000 --> 00:59:50,960 was to 1460 00:59:50,960 --> 00:59:53,760 run that then you can see that i receive 1461 00:59:53,760 --> 00:59:56,400 some dictionaries in separated lines and 1462 00:59:56,400 --> 00:59:59,119 that is because i iterate over a list of 1463 00:59:59,119 --> 01:00:01,440 dictionaries in here and that is just 1464 01:00:01,440 --> 01:00:03,599 perfect all right so the only thing that 1465 01:00:03,599 --> 01:00:07,040 we miss right now is creating instances 1466 01:00:07,040 --> 01:00:09,599 now besides printing those then we could 1467 01:00:09,599 --> 01:00:12,880 now say something like item and open up 1468 01:00:12,880 --> 01:00:14,400 and close parentheses 1469 01:00:14,400 --> 01:00:16,480 and this should be enough to instantiate 1470 01:00:16,480 --> 01:00:19,359 our instances now i can go ahead and 1471 01:00:19,359 --> 01:00:22,400 pass my arguments in here by basically 1472 01:00:22,400 --> 01:00:25,440 reading the keys from a dictionary so i 1473 01:00:25,440 --> 01:00:27,280 can say name 1474 01:00:27,280 --> 01:00:30,319 is equal to item dot get 1475 01:00:30,319 --> 01:00:32,079 and that will 1476 01:00:32,079 --> 01:00:35,359 receive name and now i can add a comma 1477 01:00:35,359 --> 01:00:39,280 and duplicate this line twice and change 1478 01:00:39,280 --> 01:00:42,319 those accordingly so this will be price 1479 01:00:42,319 --> 01:00:45,200 and this will be quantity and now i need 1480 01:00:45,200 --> 01:00:48,400 to replace my key names so it will be 1481 01:00:48,400 --> 01:00:52,480 price here and then quantity right there 1482 01:00:52,480 --> 01:00:54,480 and now let's go ahead and see what will 1483 01:00:54,480 --> 01:00:57,839 happen if i was to call this method and 1484 01:00:57,839 --> 01:01:00,559 as well as calling the attribute of 1485 01:01:00,559 --> 01:01:02,319 item.all 1486 01:01:02,319 --> 01:01:04,480 because this one stores all the 1487 01:01:04,480 --> 01:01:07,359 instances inside the list now if i was 1488 01:01:07,359 --> 01:01:10,160 to go ahead and run it then you can see 1489 01:01:10,160 --> 01:01:13,680 that i have some errors now you'll see 1490 01:01:13,680 --> 01:01:18,240 that the errors are related to the price 1491 01:01:18,240 --> 01:01:20,480 and you can see that we receive is not 1492 01:01:20,480 --> 01:01:23,599 greater than or equal to zero now let's 1493 01:01:23,599 --> 01:01:25,680 go ahead and fix this very quickly so in 1494 01:01:25,680 --> 01:01:28,640 the items.csv you can see that those are 1495 01:01:28,640 --> 01:01:31,599 actually integers that are greater than 1496 01:01:31,599 --> 01:01:34,240 zero so the problem is probably the fact 1497 01:01:34,240 --> 01:01:37,280 that those are passed as strings so we 1498 01:01:37,280 --> 01:01:40,079 need to go ahead and pass those as 1499 01:01:40,079 --> 01:01:43,200 integers so i'm going to convert those 1500 01:01:43,200 --> 01:01:44,160 into 1501 01:01:44,160 --> 01:01:45,040 int 1502 01:01:45,040 --> 01:01:47,119 like that and 1503 01:01:47,119 --> 01:01:48,799 now let's go ahead and see if we will 1504 01:01:48,799 --> 01:01:50,960 have any problems as i expect to have 1505 01:01:50,960 --> 01:01:53,440 any problem because the quantity should 1506 01:01:53,440 --> 01:01:55,359 complain about the same thing and you 1507 01:01:55,359 --> 01:01:57,039 can see that this is exactly what is 1508 01:01:57,039 --> 01:02:00,960 going on here so i can use the same for 1509 01:02:00,960 --> 01:02:02,799 quantity 1510 01:02:02,799 --> 01:02:05,200 like that and 1511 01:02:05,200 --> 01:02:08,000 work with that and you can see that now 1512 01:02:08,000 --> 01:02:11,039 we see our instances perfectly now i 1513 01:02:11,039 --> 01:02:12,559 want to show one more problem that we 1514 01:02:12,559 --> 01:02:14,480 could have in the future and we should 1515 01:02:14,480 --> 01:02:17,280 avoid now so those three lines are going 1516 01:02:17,280 --> 01:02:19,359 to work with this 1517 01:02:19,359 --> 01:02:22,559 structure of data but if i was to change 1518 01:02:22,559 --> 01:02:25,280 the price of our keyboard to something 1519 01:02:25,280 --> 01:02:27,240 like 1520 01:02:27,240 --> 01:02:30,000 74.90 something like that 1521 01:02:30,000 --> 01:02:33,119 and re-execute our 1522 01:02:33,119 --> 01:02:34,880 file then you can see that we will 1523 01:02:34,880 --> 01:02:37,119 receive some problems so we need to 1524 01:02:37,119 --> 01:02:39,520 convert the price not to an integer but 1525 01:02:39,520 --> 01:02:42,480 to a float like that and that is the 1526 01:02:42,480 --> 01:02:44,559 only way to get over this because we 1527 01:02:44,559 --> 01:02:46,319 don't want to convert the price to an 1528 01:02:46,319 --> 01:02:48,880 integer directly because it could be 1529 01:02:48,880 --> 01:02:52,720 float so now we could go ahead and 1530 01:02:52,720 --> 01:02:54,480 execute and you can see that now it 1531 01:02:54,480 --> 01:02:57,680 works perfectly although we see the 1532 01:02:57,680 --> 01:03:00,640 prices as 100.0 1533 01:03:00,640 --> 01:03:02,480 but that is something that we will look 1534 01:03:02,480 --> 01:03:04,319 into it in the future but for now it 1535 01:03:04,319 --> 01:03:06,240 works perfect and now we are ready to 1536 01:03:06,240 --> 01:03:09,280 jump on to our next topic okay so now 1537 01:03:09,280 --> 01:03:11,200 that we completely understood the class 1538 01:03:11,200 --> 01:03:13,039 methods let's go ahead and also 1539 01:03:13,039 --> 01:03:15,839 understand what static methods are now a 1540 01:03:15,839 --> 01:03:18,079 static method should do some work for 1541 01:03:18,079 --> 01:03:20,720 you that has some logical connection to 1542 01:03:20,720 --> 01:03:23,599 a class so for example if you want to 1543 01:03:23,599 --> 01:03:26,000 check if a number is an integer or a 1544 01:03:26,000 --> 01:03:28,400 float then this is a good candidate for 1545 01:03:28,400 --> 01:03:30,960 creating a static method because this 1546 01:03:30,960 --> 01:03:33,520 has some connection to the class that we 1547 01:03:33,520 --> 01:03:35,839 work with so it makes sense to check if 1548 01:03:35,839 --> 01:03:39,920 a price of an item has a decimal point 1549 01:03:39,920 --> 01:03:42,880 and by saying has a decimal point i 1550 01:03:42,880 --> 01:03:46,160 obviously count out those that are point 1551 01:03:46,160 --> 01:03:49,440 zero now to be honest static and class 1552 01:03:49,440 --> 01:03:51,839 methods could look very alike to you but 1553 01:03:51,839 --> 01:03:54,000 we will explain the main differences 1554 01:03:54,000 --> 01:03:57,200 very soon okay so i will use those lines 1555 01:03:57,200 --> 01:04:00,000 to create our first static method now 1556 01:04:00,000 --> 01:04:02,960 let's go ahead and use the def keyword 1557 01:04:02,960 --> 01:04:05,039 and we will name this method is 1558 01:04:05,039 --> 01:04:06,880 underscore integer because we said that 1559 01:04:06,880 --> 01:04:08,559 we'd like to write a static method that 1560 01:04:08,559 --> 01:04:11,119 will check if a received number is an 1561 01:04:11,119 --> 01:04:13,520 integer or not now if i was to open up 1562 01:04:13,520 --> 01:04:15,280 and close parentheses this would 1563 01:04:15,280 --> 01:04:17,760 obviously receive itself now i want you 1564 01:04:17,760 --> 01:04:20,160 to take a closer look what will happen 1565 01:04:20,160 --> 01:04:21,760 if i was to change this method into 1566 01:04:21,760 --> 01:04:23,839 being a static method and the approach 1567 01:04:23,839 --> 01:04:25,440 is going to be pretty much the same like 1568 01:04:25,440 --> 01:04:26,400 we have done 1569 01:04:26,400 --> 01:04:28,480 with the class method we will use a 1570 01:04:28,480 --> 01:04:31,039 decorator that is called static method 1571 01:04:31,039 --> 01:04:32,720 and this should be responsible to the 1572 01:04:32,720 --> 01:04:35,680 conversion so i will go ahead and 1573 01:04:35,680 --> 01:04:39,039 use this line and i will say at static 1574 01:04:39,039 --> 01:04:42,400 method like that now pay attention 1575 01:04:42,400 --> 01:04:45,839 how the received perimeter turned into 1576 01:04:45,839 --> 01:04:48,000 the regular orange color that we are 1577 01:04:48,000 --> 01:04:50,480 familiar because that is just a regular 1578 01:04:50,480 --> 01:04:53,599 parameter that we receive now this means 1579 01:04:53,599 --> 01:04:56,480 that the static methods are never 1580 01:04:56,480 --> 01:04:59,760 sending in the background the instance 1581 01:04:59,760 --> 01:05:03,520 as a first argument and that is unlike 1582 01:05:03,520 --> 01:05:06,400 the class methods the class methods are 1583 01:05:06,400 --> 01:05:09,680 sending the class reference as a first 1584 01:05:09,680 --> 01:05:11,760 argument and that is why we had to 1585 01:05:11,760 --> 01:05:15,039 receive the cls and that is why it is 1586 01:05:15,039 --> 01:05:17,599 intentionally colored with purple but 1587 01:05:17,599 --> 01:05:20,319 with static methods we never send the 1588 01:05:20,319 --> 01:05:23,920 object as the first argument so that is 1589 01:05:23,920 --> 01:05:26,720 why we should relate to the static 1590 01:05:26,720 --> 01:05:29,839 method like a regular function that just 1591 01:05:29,839 --> 01:05:32,079 receives parameters like we are familiar 1592 01:05:32,079 --> 01:05:34,640 with isolated functions now i will go 1593 01:05:34,640 --> 01:05:36,880 deeper on this just in a few minutes but 1594 01:05:36,880 --> 01:05:38,799 let's go ahead and finish up our static 1595 01:05:38,799 --> 01:05:41,599 method first so this should receive 1596 01:05:41,599 --> 01:05:44,079 num as one parameter because we should 1597 01:05:44,079 --> 01:05:45,680 receive at least something to check if 1598 01:05:45,680 --> 01:05:48,079 it is an integer or not all right so now 1599 01:05:48,079 --> 01:05:50,400 that we are inside this method 1600 01:05:50,400 --> 01:05:52,480 then i can go ahead and use a couple of 1601 01:05:52,480 --> 01:05:54,880 if statements to check if the received 1602 01:05:54,880 --> 01:05:57,520 argument is an integer or not now if you 1603 01:05:57,520 --> 01:06:00,079 remember we said that we'd like to 1604 01:06:00,079 --> 01:06:02,079 we will count 1605 01:06:02,079 --> 01:06:04,960 out the floats that are 1606 01:06:04,960 --> 01:06:08,640 decimal that are 0.0 okay 1607 01:06:08,640 --> 01:06:10,400 meaning 1608 01:06:10,400 --> 01:06:13,200 for example 5.0 1609 01:06:13,200 --> 01:06:16,559 10.0 and so on all right so now that we 1610 01:06:16,559 --> 01:06:19,119 understood this let's go ahead and use 1611 01:06:19,119 --> 01:06:21,599 an if statement here so if and we will 1612 01:06:21,599 --> 01:06:23,359 call the built in function that is 1613 01:06:23,359 --> 01:06:25,680 called is instance 1614 01:06:25,680 --> 01:06:28,319 and this should receive two arguments 1615 01:06:28,319 --> 01:06:30,000 and we can understand what this function 1616 01:06:30,000 --> 01:06:32,079 is going to do for us it is going to 1617 01:06:32,079 --> 01:06:33,839 check if the 1618 01:06:33,839 --> 01:06:37,280 received parameter is an instance of a 1619 01:06:37,280 --> 01:06:40,640 float or an integer so we will pass in 1620 01:06:40,640 --> 01:06:43,200 as the first argument the num and as the 1621 01:06:43,200 --> 01:06:45,039 second argument the flow 1622 01:06:45,039 --> 01:06:47,280 without calling those parentheses so 1623 01:06:47,280 --> 01:06:50,720 only the reference to the float keyword 1624 01:06:50,720 --> 01:06:53,680 so this conditional should go ahead and 1625 01:06:53,680 --> 01:06:56,400 check if the num is a floating number or 1626 01:06:56,400 --> 01:06:57,200 not 1627 01:06:57,200 --> 01:07:00,000 now inside this if statement i will say 1628 01:07:00,000 --> 01:07:01,039 return 1629 01:07:01,039 --> 01:07:02,000 num 1630 01:07:02,000 --> 01:07:03,359 dot is 1631 01:07:03,359 --> 01:07:06,960 integer so by saying dot is integer then 1632 01:07:06,960 --> 01:07:08,640 i basically say 1633 01:07:08,640 --> 01:07:12,720 count out the floats that are decimal 1634 01:07:12,720 --> 01:07:15,200 that are point zero 1635 01:07:15,200 --> 01:07:17,839 so this means that if i was to pass in 1636 01:07:17,839 --> 01:07:21,039 here a number like 10.0 then this will 1637 01:07:21,039 --> 01:07:23,760 return false but remember that this will 1638 01:07:23,760 --> 01:07:25,520 enter here because it thinks it is a 1639 01:07:25,520 --> 01:07:27,520 float because it is represented in that 1640 01:07:27,520 --> 01:07:30,720 way and so the is underscore integer 1641 01:07:30,720 --> 01:07:33,920 should check if the point is zero and it 1642 01:07:33,920 --> 01:07:36,240 should return false accordingly now i 1643 01:07:36,240 --> 01:07:38,640 will also use an else if statement here 1644 01:07:38,640 --> 01:07:41,119 to basically check if it is integer by 1645 01:07:41,119 --> 01:07:45,280 itself so i will say el if is 1646 01:07:45,280 --> 01:07:46,799 instance num 1647 01:07:46,799 --> 01:07:49,200 and check if it is an instance of an 1648 01:07:49,200 --> 01:07:53,520 integer then i will just return true and 1649 01:07:53,520 --> 01:07:55,599 if it is just something else then i will 1650 01:07:55,599 --> 01:07:56,880 just say 1651 01:07:56,880 --> 01:07:59,920 return false like that so now that we 1652 01:07:59,920 --> 01:08:01,839 have designed this method then let's 1653 01:08:01,839 --> 01:08:05,359 take a look how we can call it so now i 1654 01:08:05,359 --> 01:08:08,319 will just remove this and this i'm not 1655 01:08:08,319 --> 01:08:10,079 actually going to instantiate anything 1656 01:08:10,079 --> 01:08:12,319 i'm just going to show you how you can 1657 01:08:12,319 --> 01:08:14,799 access to the static method so i will 1658 01:08:14,799 --> 01:08:19,120 just call this item dot is integer and i 1659 01:08:19,120 --> 01:08:21,520 will just pass in a number that i would 1660 01:08:21,520 --> 01:08:24,080 like to check if it is an integer or not 1661 01:08:24,080 --> 01:08:26,799 now for sure we like to print this so we 1662 01:08:26,799 --> 01:08:28,880 will see the result now let's go ahead 1663 01:08:28,880 --> 01:08:30,799 and pass in seven 1664 01:08:30,799 --> 01:08:33,520 so you can see that we receive true now 1665 01:08:33,520 --> 01:08:36,319 if i was to pass in 7.5 then i would 1666 01:08:36,319 --> 01:08:38,640 receive false and what is happening in 1667 01:08:38,640 --> 01:08:40,880 the background it is the fact that it 1668 01:08:40,880 --> 01:08:44,158 enters here but it sees that it is not 1669 01:08:44,158 --> 01:08:47,279 an integer so it returns false but if i 1670 01:08:47,279 --> 01:08:50,319 was to change this to 7.0 1671 01:08:50,319 --> 01:08:53,120 then this should sorry about that this 1672 01:08:53,120 --> 01:08:54,960 should still return 1673 01:08:54,960 --> 01:08:57,198 true because what is happening it is 1674 01:08:57,198 --> 01:08:59,600 entering inside this conditional and 1675 01:08:59,600 --> 01:09:02,158 then it checks if it is an integer but 1676 01:09:02,158 --> 01:09:04,479 we said that this method counts out the 1677 01:09:04,479 --> 01:09:08,158 floats that are 0.0 so it returns true 1678 01:09:08,158 --> 01:09:10,479 still so that is a perfect design all 1679 01:09:10,479 --> 01:09:13,439 right so i have came up with a new file 1680 01:09:13,439 --> 01:09:15,359 which i will just explain here when to 1681 01:09:15,359 --> 01:09:17,198 use a class method and when to use a 1682 01:09:17,198 --> 01:09:19,120 static method so we can completely 1683 01:09:19,120 --> 01:09:20,799 understand the differences between those 1684 01:09:20,799 --> 01:09:22,799 because i remember myself i had a very 1685 01:09:22,799 --> 01:09:25,679 tough time to understand why i need this 1686 01:09:25,679 --> 01:09:28,238 and why i need the other one so that 1687 01:09:28,238 --> 01:09:29,600 will be the main question that i will be 1688 01:09:29,600 --> 01:09:31,679 answering in this python file so don't 1689 01:09:31,679 --> 01:09:33,920 feel like you have to copy and paste the 1690 01:09:33,920 --> 01:09:35,839 code following along what i am 1691 01:09:35,839 --> 01:09:37,679 explaining here by listening should be 1692 01:09:37,679 --> 01:09:40,238 enough all right so in this file i will 1693 01:09:40,238 --> 01:09:43,520 just go ahead and create this class item 1694 01:09:43,520 --> 01:09:45,040 that we have 1695 01:09:45,040 --> 01:09:47,679 right and i will use pass to not receive 1696 01:09:47,679 --> 01:09:50,640 errors now when we will use a static 1697 01:09:50,640 --> 01:09:53,600 method so we will use a static method 1698 01:09:53,600 --> 01:09:56,880 when we want to do something that should 1699 01:09:56,880 --> 01:09:59,679 not be unique per instance exactly like 1700 01:09:59,679 --> 01:10:02,880 we have done previously so each integer 1701 01:10:02,880 --> 01:10:05,040 is a method that is just going to be 1702 01:10:05,040 --> 01:10:07,040 responsible to check if a number is 1703 01:10:07,040 --> 01:10:09,360 integer or not so that is why i could 1704 01:10:09,360 --> 01:10:12,239 allow myself to include this under the 1705 01:10:12,239 --> 01:10:15,280 item just like i could use this def as 1706 01:10:15,280 --> 01:10:17,360 an isolated function right above the 1707 01:10:17,360 --> 01:10:20,480 class and that was also okay but i 1708 01:10:20,480 --> 01:10:23,120 prefer to not do that because although 1709 01:10:23,120 --> 01:10:25,840 this is a method that has nothing to do 1710 01:10:25,840 --> 01:10:29,199 with instance that is somehow related to 1711 01:10:29,199 --> 01:10:32,640 the item class so that is the reason you 1712 01:10:32,640 --> 01:10:35,440 want to create this as a static method 1713 01:10:35,440 --> 01:10:38,480 like we have designed previously and the 1714 01:10:38,480 --> 01:10:40,719 reason that you would like to create a 1715 01:10:40,719 --> 01:10:42,000 class method 1716 01:10:42,000 --> 01:10:45,520 is for instantiating instances from some 1717 01:10:45,520 --> 01:10:48,880 structured data that you own so exactly 1718 01:10:48,880 --> 01:10:50,719 like we have done we have created a 1719 01:10:50,719 --> 01:10:53,120 class method that was responsible to 1720 01:10:53,120 --> 01:10:56,320 read the csv file and creating some 1721 01:10:56,320 --> 01:11:00,080 instances so as i wrote here those are 1722 01:11:00,080 --> 01:11:02,800 used to manipulate different structures 1723 01:11:02,800 --> 01:11:05,840 of data to instantiate objects like we 1724 01:11:05,840 --> 01:11:08,000 have done with the csv file we could 1725 01:11:08,000 --> 01:11:10,320 also use a class method like install 1726 01:11:10,320 --> 01:11:12,560 sheet from a json file 1727 01:11:12,560 --> 01:11:15,920 or from a yaml file those just are 1728 01:11:15,920 --> 01:11:18,800 different ways to maintain data in the 1729 01:11:18,800 --> 01:11:20,960 best practice way and that is the code 1730 01:11:20,960 --> 01:11:23,040 that you look to include inside your 1731 01:11:23,040 --> 01:11:26,000 class methods that is why they should be 1732 01:11:26,000 --> 01:11:28,640 existing in any class especially if you 1733 01:11:28,640 --> 01:11:31,280 look to instantiate hundreds of objects 1734 01:11:31,280 --> 01:11:33,600 on your programs so it is a great idea 1735 01:11:33,600 --> 01:11:36,560 to have at least one class method like 1736 01:11:36,560 --> 01:11:40,159 we have done in the item class now the 1737 01:11:40,159 --> 01:11:43,520 only main difference between a class 1738 01:11:43,520 --> 01:11:46,880 method and to a static method is the 1739 01:11:46,880 --> 01:11:49,600 fact that static methods are not passing 1740 01:11:49,600 --> 01:11:51,760 the object reference as the first 1741 01:11:51,760 --> 01:11:54,000 argument in the background it is 1742 01:11:54,000 --> 01:11:56,080 noticeable from the fact that we don't 1743 01:11:56,080 --> 01:11:58,719 have a special highlight purple in my 1744 01:11:58,719 --> 01:12:02,159 case for the first parameter so if you 1745 01:12:02,159 --> 01:12:04,640 remember if i was to go ahead and use 1746 01:12:04,640 --> 01:12:07,199 here a first parameter like num then you 1747 01:12:07,199 --> 01:12:08,719 will see that this is the first 1748 01:12:08,719 --> 01:12:10,719 parameter that is colored with orange 1749 01:12:10,719 --> 01:12:13,040 because that is a regular parameter but 1750 01:12:13,040 --> 01:12:15,520 that is purple because this is a 1751 01:12:15,520 --> 01:12:17,280 mandatory parameter that we should 1752 01:12:17,280 --> 01:12:19,360 receive because what i have just 1753 01:12:19,360 --> 01:12:21,679 explained so those are the main 1754 01:12:21,679 --> 01:12:24,560 differences between a static method to a 1755 01:12:24,560 --> 01:12:26,480 class method now if you remember i 1756 01:12:26,480 --> 01:12:28,640 intentionally said that the class 1757 01:12:28,640 --> 01:12:30,560 methods and the static methods could 1758 01:12:30,560 --> 01:12:33,280 only be called from the class level but 1759 01:12:33,280 --> 01:12:36,159 however those also could be called from 1760 01:12:36,159 --> 01:12:39,040 instances so as you can see i can 1761 01:12:39,040 --> 01:12:41,280 actually instantiate an object and call 1762 01:12:41,280 --> 01:12:42,960 the is integer and as well as the 1763 01:12:42,960 --> 01:12:44,800 instant sheet from something you can 1764 01:12:44,800 --> 01:12:46,719 just pass in here a number like five and 1765 01:12:46,719 --> 01:12:48,880 i will not receive any errors and if i 1766 01:12:48,880 --> 01:12:50,719 was to run the helper then you can see 1767 01:12:50,719 --> 01:12:53,120 that i don't have any error now i'm 1768 01:12:53,120 --> 01:12:55,360 going to be honest with you i never saw 1769 01:12:55,360 --> 01:12:58,400 a reason to call a static method or a 1770 01:12:58,400 --> 01:13:00,880 class method from the instance level but 1771 01:13:00,880 --> 01:13:02,960 that is just an option that exists i 1772 01:13:02,960 --> 01:13:05,520 know that it is very very confusing but 1773 01:13:05,520 --> 01:13:07,280 that is something you are rarely going 1774 01:13:07,280 --> 01:13:09,679 to see and like i said i never saw a 1775 01:13:09,679 --> 01:13:12,400 great reason to call a static method or 1776 01:13:12,400 --> 01:13:15,440 to call a class method from an instance 1777 01:13:15,440 --> 01:13:17,679 so my recommendation to not confuse you 1778 01:13:17,679 --> 01:13:20,159 is just not going with calling those 1779 01:13:20,159 --> 01:13:22,320 from the instance level alright so i 1780 01:13:22,320 --> 01:13:24,480 minimized the code that we wrote so far 1781 01:13:24,480 --> 01:13:27,040 in the class item now in order to start 1782 01:13:27,040 --> 01:13:28,880 solving the problems that we will solve 1783 01:13:28,880 --> 01:13:30,960 in this episode then i'm going to create 1784 01:13:30,960 --> 01:13:33,360 here two instances so i will say phone 1785 01:13:33,360 --> 01:13:34,239 one 1786 01:13:34,239 --> 01:13:37,040 is equal to an item and let's give it a 1787 01:13:37,040 --> 01:13:40,400 name like jsc phone v10 1788 01:13:40,400 --> 01:13:42,239 and then just 1789 01:13:42,239 --> 01:13:45,040 use a random price and quantity and i 1790 01:13:45,040 --> 01:13:47,679 will copy and paste this and use another 1791 01:13:47,679 --> 01:13:50,239 variable like phone 2 and i will 1792 01:13:50,239 --> 01:13:52,159 increase the version by 10 and let's say 1793 01:13:52,159 --> 01:13:54,640 that this price for the phone 2 should 1794 01:13:54,640 --> 01:13:57,280 be 700 all right so now that we have 1795 01:13:57,280 --> 01:13:59,679 created two instances of a phone pay 1796 01:13:59,679 --> 01:14:02,159 attention that those two items are 1797 01:14:02,159 --> 01:14:04,239 phones so we could think about some 1798 01:14:04,239 --> 01:14:07,120 attributes that could represent phones 1799 01:14:07,120 --> 01:14:09,679 in real life think about an attribute 1800 01:14:09,679 --> 01:14:11,840 like broken phones because we could have 1801 01:14:11,840 --> 01:14:14,239 some phones that could have been broken 1802 01:14:14,239 --> 01:14:16,800 and so we cannot really mark it as a 1803 01:14:16,800 --> 01:14:19,199 phone that we could really sell so this 1804 01:14:19,199 --> 01:14:21,600 means that we could go ahead and say 1805 01:14:21,600 --> 01:14:24,239 phone one dot broken phones 1806 01:14:24,239 --> 01:14:26,560 let's say that we have unfortunately one 1807 01:14:26,560 --> 01:14:28,960 broken phone on our hand right now so i 1808 01:14:28,960 --> 01:14:30,480 will go ahead and design the same 1809 01:14:30,480 --> 01:14:32,719 attribute for our second phone and now 1810 01:14:32,719 --> 01:14:35,120 that we have came up with this realistic 1811 01:14:35,120 --> 01:14:37,199 attribute then the next step that we 1812 01:14:37,199 --> 01:14:39,360 might think about could be creating a 1813 01:14:39,360 --> 01:14:41,440 method that will go ahead and calculate 1814 01:14:41,440 --> 01:14:43,760 the phones that are actually not broken 1815 01:14:43,760 --> 01:14:46,719 meaning subtracting the quantity by the 1816 01:14:46,719 --> 01:14:48,560 broken phones amount because this is 1817 01:14:48,560 --> 01:14:50,320 totally making sense and then we can 1818 01:14:50,320 --> 01:14:52,400 understand what are the phones that we 1819 01:14:52,400 --> 01:14:55,120 could go actually and sell them in the 1820 01:14:55,120 --> 01:14:57,440 future but we have a couple of problems 1821 01:14:57,440 --> 01:14:59,199 creating a method that will go ahead and 1822 01:14:59,199 --> 01:15:01,679 calculate such a thing because we cannot 1823 01:15:01,679 --> 01:15:04,080 really go ahead inside our item and do 1824 01:15:04,080 --> 01:15:06,400 this smooth enough because we don't 1825 01:15:06,400 --> 01:15:09,040 really have the broken phones attribute 1826 01:15:09,040 --> 01:15:12,159 assigned to self and we cannot actually 1827 01:15:12,159 --> 01:15:13,920 go ahead and create this method inside 1828 01:15:13,920 --> 01:15:16,640 this item class because this method is 1829 01:15:16,640 --> 01:15:18,640 not going to be useful for other 1830 01:15:18,640 --> 01:15:20,560 hundreds of items that you will go ahead 1831 01:15:20,560 --> 01:15:23,440 and create this just represent a phone 1832 01:15:23,440 --> 01:15:26,320 kind of item so in order to solve this 1833 01:15:26,320 --> 01:15:29,120 problem in terms of best practices in 1834 01:15:29,120 --> 01:15:31,120 object oriented programming then we 1835 01:15:31,120 --> 01:15:33,120 could go ahead and create a separated 1836 01:15:33,120 --> 01:15:34,920 class that will inherit the 1837 01:15:34,920 --> 01:15:37,760 functionalities that the item class 1838 01:15:37,760 --> 01:15:39,600 brings with it and that is exactly where 1839 01:15:39,600 --> 01:15:42,239 we could benefit from inheritance and we 1840 01:15:42,239 --> 01:15:43,920 could go ahead and create a separated 1841 01:15:43,920 --> 01:15:46,239 class that we could name phone and then 1842 01:15:46,239 --> 01:15:49,600 this phone class will inherit all the 1843 01:15:49,600 --> 01:15:52,719 methods and all the attributes that item 1844 01:15:52,719 --> 01:15:55,840 class has so let's go ahead and simulate 1845 01:15:55,840 --> 01:15:57,920 that so i'm not going to delete the 1846 01:15:57,920 --> 01:16:00,560 instances yet but i'm going to go ahead 1847 01:16:00,560 --> 01:16:03,520 here and create a class that i will name 1848 01:16:03,520 --> 01:16:06,239 it phone now pay attention that i will 1849 01:16:06,239 --> 01:16:08,560 not use a semicolon and i will use those 1850 01:16:08,560 --> 01:16:12,320 brackets and i will specify what class i 1851 01:16:12,320 --> 01:16:14,480 would like to inherit from so i will 1852 01:16:14,480 --> 01:16:18,239 inherit from item and then i will just 1853 01:16:18,239 --> 01:16:20,239 use a pass temporarily because i would 1854 01:16:20,239 --> 01:16:22,400 not like to use additional functionality 1855 01:16:22,400 --> 01:16:24,719 right now inside this class okay so now 1856 01:16:24,719 --> 01:16:26,719 that we have created this class then 1857 01:16:26,719 --> 01:16:28,880 let's go ahead and first execute our 1858 01:16:28,880 --> 01:16:31,440 program where at the first stage the 1859 01:16:31,440 --> 01:16:34,480 instances will be item instances 1860 01:16:34,480 --> 01:16:36,400 and this should not have any problems 1861 01:16:36,400 --> 01:16:38,640 because we know that we can create those 1862 01:16:38,640 --> 01:16:40,640 item instances and we will not receive 1863 01:16:40,640 --> 01:16:42,960 any errors but if we were to change 1864 01:16:42,960 --> 01:16:44,719 those to 1865 01:16:44,719 --> 01:16:46,480 phone 1866 01:16:46,480 --> 01:16:49,280 like that then we should still not 1867 01:16:49,280 --> 01:16:51,920 receive any arrows and that's just a 1868 01:16:51,920 --> 01:16:54,400 basic way that you could use inheritance 1869 01:16:54,400 --> 01:16:57,280 in order to represent different kinds of 1870 01:16:57,280 --> 01:16:59,760 objects when you want to do that now 1871 01:16:59,760 --> 01:17:01,600 this could also be applied to other 1872 01:17:01,600 --> 01:17:03,440 realistic programs that you want to come 1873 01:17:03,440 --> 01:17:06,080 up with them by your own but in my case 1874 01:17:06,080 --> 01:17:08,560 it totally makes sense to create some 1875 01:17:08,560 --> 01:17:11,280 classes where each class will represent 1876 01:17:11,280 --> 01:17:13,840 a kind of an item and then i could go 1877 01:17:13,840 --> 01:17:16,480 ahead and inherit from the item class 1878 01:17:16,480 --> 01:17:18,960 in each of the child classes that i will 1879 01:17:18,960 --> 01:17:20,800 go ahead and create in the future i 1880 01:17:20,800 --> 01:17:23,280 could also use another class for a kind 1881 01:17:23,280 --> 01:17:25,840 of item like laptop and then i could go 1882 01:17:25,840 --> 01:17:27,679 ahead and use a separated functionality 1883 01:17:27,679 --> 01:17:30,239 for that now when we talk about classes 1884 01:17:30,239 --> 01:17:32,880 that we inherit from then those are 1885 01:17:32,880 --> 01:17:35,360 considered to be called parent classes 1886 01:17:35,360 --> 01:17:38,400 and when we use multiple classes that 1887 01:17:38,400 --> 01:17:40,640 inherits from that parent class then 1888 01:17:40,640 --> 01:17:43,040 those are considered to be called child 1889 01:17:43,040 --> 01:17:45,760 classes so those are just terms that you 1890 01:17:45,760 --> 01:17:47,360 want to be familiar with when we talk 1891 01:17:47,360 --> 01:17:49,520 about object-oriented programming and 1892 01:17:49,520 --> 01:17:51,760 from here we will see more advanced 1893 01:17:51,760 --> 01:17:54,159 things that you can go ahead and do with 1894 01:17:54,159 --> 01:17:56,880 your child classes all right so now 1895 01:17:56,880 --> 01:17:58,400 let's go ahead and understand some more 1896 01:17:58,400 --> 01:18:00,719 advanced things about inheritance now 1897 01:18:00,719 --> 01:18:02,640 throughout this series we learned that 1898 01:18:02,640 --> 01:18:04,719 it is not a great idea to assign 1899 01:18:04,719 --> 01:18:07,920 attributes manually once we create those 1900 01:18:07,920 --> 01:18:10,159 instances and the better way to do that 1901 01:18:10,159 --> 01:18:12,320 is actually going ahead and creating our 1902 01:18:12,320 --> 01:18:15,360 constructor and pass the value that we 1903 01:18:15,360 --> 01:18:18,320 like to immediately in the instance 1904 01:18:18,320 --> 01:18:21,280 creation exactly like here so in order 1905 01:18:21,280 --> 01:18:23,600 to solve this then we gonna need to 1906 01:18:23,600 --> 01:18:25,280 figure out how we are going to do that 1907 01:18:25,280 --> 01:18:27,679 because creating the constructor inside 1908 01:18:27,679 --> 01:18:29,679 this phone class is going to be a little 1909 01:18:29,679 --> 01:18:31,840 bit tricky because we don't really want 1910 01:18:31,840 --> 01:18:34,320 to break the logic that the development 1911 01:18:34,320 --> 01:18:36,800 score in it brings with the parent class 1912 01:18:36,800 --> 01:18:38,719 but we'd also like to pass in an 1913 01:18:38,719 --> 01:18:41,520 additional attribute like broken phones 1914 01:18:41,520 --> 01:18:44,719 that we will go ahead and deal with that 1915 01:18:44,719 --> 01:18:46,640 attribute and assign it to the self 1916 01:18:46,640 --> 01:18:49,840 object exactly like we have done in the 1917 01:18:49,840 --> 01:18:52,719 second part of our series so in order to 1918 01:18:52,719 --> 01:18:55,120 keep the logic the same for this child 1919 01:18:55,120 --> 01:18:57,520 class and as well as receive some more 1920 01:18:57,520 --> 01:19:00,640 attributes then for now i'm going to go 1921 01:19:00,640 --> 01:19:03,760 ahead and copy the code in our 1922 01:19:03,760 --> 01:19:07,679 constructor and just paste this in 1923 01:19:07,679 --> 01:19:10,880 right inside our phone class and that's 1924 01:19:10,880 --> 01:19:14,000 making sense temporarily because we 1925 01:19:14,000 --> 01:19:16,800 receive the exact same parameters that 1926 01:19:16,800 --> 01:19:19,760 we should receive when we instantiate an 1927 01:19:19,760 --> 01:19:22,000 instance and we also have now the 1928 01:19:22,000 --> 01:19:25,520 control to receive some more parameters 1929 01:19:25,520 --> 01:19:27,679 like we want to do with the broken 1930 01:19:27,679 --> 01:19:31,199 phones so let's go ahead here and say 1931 01:19:31,199 --> 01:19:33,199 broken 1932 01:19:33,199 --> 01:19:35,679 so i will just scroll here and i will 1933 01:19:35,679 --> 01:19:39,040 say broken phones is equal to zero let's 1934 01:19:39,040 --> 01:19:41,600 also receive a default value for that 1935 01:19:41,600 --> 01:19:43,600 and let's go ahead and type in a 1936 01:19:43,600 --> 01:19:46,480 validation for the broken phones so i 1937 01:19:46,480 --> 01:19:48,640 will allow myself to just copy that and 1938 01:19:48,640 --> 01:19:51,840 paste this in and we'll use a third 1939 01:19:51,840 --> 01:19:54,400 quantity i mean broken phones is greater 1940 01:19:54,400 --> 01:19:57,040 than or equal to zero and i will change 1941 01:19:57,040 --> 01:19:58,400 this to 1942 01:19:58,400 --> 01:20:01,440 broken phone 1943 01:20:01,920 --> 01:20:04,080 like that actually broken phones and 1944 01:20:04,080 --> 01:20:05,440 this should 1945 01:20:05,440 --> 01:20:07,679 be exactly like we have done with the 1946 01:20:07,679 --> 01:20:09,920 quantity and now let's go ahead to the 1947 01:20:09,920 --> 01:20:12,800 section of assign to self object and we 1948 01:20:12,800 --> 01:20:15,520 can use self.broken 1949 01:20:15,520 --> 01:20:18,960 phones is equal to broken phones like 1950 01:20:18,960 --> 01:20:19,760 that 1951 01:20:19,760 --> 01:20:21,679 and you can see that here we have 1952 01:20:21,679 --> 01:20:24,159 actions to execute now it could have 1953 01:20:24,159 --> 01:20:26,400 been nicer if we could also create a 1954 01:20:26,400 --> 01:20:29,840 class attribute for the phone class and 1955 01:20:29,840 --> 01:20:31,600 that will mean that we could go ahead 1956 01:20:31,600 --> 01:20:34,719 here and say all is equal to an empty 1957 01:20:34,719 --> 01:20:35,600 list 1958 01:20:35,600 --> 01:20:38,560 and then we could go ahead and 1959 01:20:38,560 --> 01:20:42,480 use a phone dot all dot append like that 1960 01:20:42,480 --> 01:20:46,480 and now if i was to go ahead and run 1961 01:20:46,480 --> 01:20:48,480 this program then you can see that i 1962 01:20:48,480 --> 01:20:50,960 will not receive any arrows now to check 1963 01:20:50,960 --> 01:20:53,679 that this works then i'm also going to 1964 01:20:53,679 --> 01:20:56,800 pass in here one and i'm going to do the 1965 01:20:56,800 --> 01:20:59,120 same here as well and i'm going to 1966 01:20:59,120 --> 01:21:01,120 remove those all right i'm going to 1967 01:21:01,120 --> 01:21:03,440 remove the hard-coded attributes and the 1968 01:21:03,440 --> 01:21:06,080 program still works now i'd also like to 1969 01:21:06,080 --> 01:21:08,159 test this by applying one of the methods 1970 01:21:08,159 --> 01:21:10,719 that we have wrote so far and that will 1971 01:21:10,719 --> 01:21:12,320 be obviously a method that i'd like to 1972 01:21:12,320 --> 01:21:14,480 use from the parent class because we 1973 01:21:14,480 --> 01:21:17,120 inherit those methods so i can go ahead 1974 01:21:17,120 --> 01:21:18,760 and use 1975 01:21:18,760 --> 01:21:20,719 phoneone dot 1976 01:21:20,719 --> 01:21:23,920 calculate total price and it makes sense 1977 01:21:23,920 --> 01:21:25,679 to print this so i will go ahead and 1978 01:21:25,679 --> 01:21:28,639 print that and you can see that print 1979 01:21:28,639 --> 01:21:31,440 phone one dot calculate total price and 1980 01:21:31,440 --> 01:21:33,360 now if i was to run that then you can 1981 01:21:33,360 --> 01:21:35,840 see that i receive a result so this 1982 01:21:35,840 --> 01:21:38,159 means that i don't have any errors now 1983 01:21:38,159 --> 01:21:40,080 i'm not sure if you paid attention to 1984 01:21:40,080 --> 01:21:42,880 this but if i was to scroll up a bit 1985 01:21:42,880 --> 01:21:44,080 then you're gonna see that the 1986 01:21:44,080 --> 01:21:46,000 constructor in the chat class is 1987 01:21:46,000 --> 01:21:48,480 complaining about something and let's 1988 01:21:48,480 --> 01:21:50,639 hover the mouse and see what is the 1989 01:21:50,639 --> 01:21:52,800 warning now you can see that it says to 1990 01:21:52,800 --> 01:21:55,280 us call to double underscore init of 1991 01:21:55,280 --> 01:21:58,719 super class is missed and what that 1992 01:21:58,719 --> 01:22:01,040 means it means that when we initialize 1993 01:22:01,040 --> 01:22:03,199 the double underscore init method inside 1994 01:22:03,199 --> 01:22:05,840 the child class then python expects for 1995 01:22:05,840 --> 01:22:08,239 some function to be called intentionally 1996 01:22:08,239 --> 01:22:11,040 now this function is named super and 1997 01:22:11,040 --> 01:22:13,199 what super allows us to do it allows us 1998 01:22:13,199 --> 01:22:15,120 to have full access to all the 1999 01:22:15,120 --> 01:22:17,360 attributes of the parent class and by 2000 01:22:17,360 --> 01:22:20,000 using the super function we don't really 2001 01:22:20,000 --> 01:22:22,320 need to hard code in the attribute 2002 01:22:22,320 --> 01:22:24,400 assignment like we have done with the 2003 01:22:24,400 --> 01:22:26,880 name price and quantity and as well as 2004 01:22:26,880 --> 01:22:28,800 for the other validations that we have 2005 01:22:28,800 --> 01:22:32,000 executed every time that we want to come 2006 01:22:32,000 --> 01:22:34,880 up with a child class now imagine how 2007 01:22:34,880 --> 01:22:37,760 hard that is going to be if for each of 2008 01:22:37,760 --> 01:22:39,679 the child classes that we will create in 2009 01:22:39,679 --> 01:22:42,080 the future we will have to go through 2010 01:22:42,080 --> 01:22:44,719 copying and pasting a third price and 2011 01:22:44,719 --> 01:22:47,040 quantity and as well as doing the 2012 01:22:47,040 --> 01:22:50,080 assigned to self objecting in those 2013 01:22:50,080 --> 01:22:52,320 three lines that is going to be a lot of 2014 01:22:52,320 --> 01:22:55,360 duplication of code now to save us that 2015 01:22:55,360 --> 01:22:58,159 time that is exactly why we need to use 2016 01:22:58,159 --> 01:23:00,560 the super function the super function 2017 01:23:00,560 --> 01:23:02,880 will allow us to have the attributes 2018 01:23:02,880 --> 01:23:05,040 access from the parent classes and 2019 01:23:05,040 --> 01:23:08,239 therefore we will be able to fully 2020 01:23:08,239 --> 01:23:10,080 implement the best practices in 2021 01:23:10,080 --> 01:23:11,840 inheritance when it comes to 2022 01:23:11,840 --> 01:23:14,560 object-oriented programs now again this 2023 01:23:14,560 --> 01:23:17,280 program works because we assign the 2024 01:23:17,280 --> 01:23:20,000 attributes of name price in quantity for 2025 01:23:20,000 --> 01:23:23,600 the self object in the child class but 2026 01:23:23,600 --> 01:23:25,679 if i was to remove 2027 01:23:25,679 --> 01:23:26,880 those 2028 01:23:26,880 --> 01:23:29,440 three lines and as well as those two 2029 01:23:29,440 --> 01:23:31,440 lines now those lines are happen to be 2030 01:23:31,440 --> 01:23:34,080 the lines that i have copied and pasted 2031 01:23:34,080 --> 01:23:36,560 and try to run this program then you can 2032 01:23:36,560 --> 01:23:38,800 see that we receive attribute error 2033 01:23:38,800 --> 01:23:41,600 phone object has no attribute price and 2034 01:23:41,600 --> 01:23:43,520 pay attention from what line it comes 2035 01:23:43,520 --> 01:23:46,400 from it comes from line 21 2036 01:23:46,400 --> 01:23:48,400 from the item class because it thinks 2037 01:23:48,400 --> 01:23:50,719 that it has the attribute of price but 2038 01:23:50,719 --> 01:23:53,360 we never have the price attribute in the 2039 01:23:53,360 --> 01:23:55,520 phone level because we just deleted the 2040 01:23:55,520 --> 01:23:58,639 self.price is equal to price and that's 2041 01:23:58,639 --> 01:23:59,440 why 2042 01:23:59,440 --> 01:24:01,600 now we have some problems and we are 2043 01:24:01,600 --> 01:24:03,600 going to replace all the lines that we 2044 01:24:03,600 --> 01:24:05,760 have deleted with the following thing 2045 01:24:05,760 --> 01:24:08,080 that i'm going to just execute now so 2046 01:24:08,080 --> 01:24:11,280 i'm going to go to the first line of our 2047 01:24:11,280 --> 01:24:15,199 constructor and i'm going to say call 2048 01:24:15,199 --> 01:24:16,719 to super 2049 01:24:16,719 --> 01:24:18,159 function 2050 01:24:18,159 --> 01:24:20,000 to have access 2051 01:24:20,000 --> 01:24:23,040 to all attributes 2052 01:24:23,040 --> 01:24:26,239 slash methods and then i'm going to say 2053 01:24:26,239 --> 01:24:27,199 super 2054 01:24:27,199 --> 01:24:28,840 then i'm going to open up and close 2055 01:24:28,840 --> 01:24:31,040 parentheses and then i'm going to use 2056 01:24:31,040 --> 01:24:33,840 the double underscore init method like 2057 01:24:33,840 --> 01:24:35,679 that now you can see that the second 2058 01:24:35,679 --> 01:24:37,600 that i have completed this then there 2059 01:24:37,600 --> 01:24:38,880 are no more warnings about the 2060 01:24:38,880 --> 01:24:40,960 constructor in this child class and you 2061 01:24:40,960 --> 01:24:42,960 can also see that this double underscore 2062 01:24:42,960 --> 01:24:45,840 init method expects for some special 2063 01:24:45,840 --> 01:24:48,080 arguments now those special arguments 2064 01:24:48,080 --> 01:24:50,239 obviously coming from the item class 2065 01:24:50,239 --> 01:24:53,440 that we inherit from so if i was to pass 2066 01:24:53,440 --> 01:24:54,800 in here 2067 01:24:54,800 --> 01:24:55,920 name 2068 01:24:55,920 --> 01:24:57,440 and also 2069 01:24:57,440 --> 01:25:01,040 price and also quantity then this should 2070 01:25:01,040 --> 01:25:04,320 be fine now you can also ask yourself 2071 01:25:04,320 --> 01:25:06,800 isn't it a duplication of code the fact 2072 01:25:06,800 --> 01:25:08,880 that we also copied and pasted the 2073 01:25:08,880 --> 01:25:11,199 parameters that we receive in the child 2074 01:25:11,199 --> 01:25:13,760 class and yeah that is a perfect 2075 01:25:13,760 --> 01:25:15,840 question that is something that could be 2076 01:25:15,840 --> 01:25:18,000 solved by something more advanced if you 2077 01:25:18,000 --> 01:25:20,480 heard about keyword arguments that is 2078 01:25:20,480 --> 01:25:22,480 something that we can solve it with that 2079 01:25:22,480 --> 01:25:24,320 way and then we will not have to 2080 01:25:24,320 --> 01:25:26,639 duplicate the parameters that we receive 2081 01:25:26,639 --> 01:25:28,400 for the constructor but that is not 2082 01:25:28,400 --> 01:25:29,920 something that i'm going to show for 2083 01:25:29,920 --> 01:25:32,400 that stage i'm going to stick with it 2084 01:25:32,400 --> 01:25:34,719 and i'm just going to leave it as it is 2085 01:25:34,719 --> 01:25:35,520 now 2086 01:25:35,520 --> 01:25:38,159 calling the super function and as well 2087 01:25:38,159 --> 01:25:40,639 as the init method right after it should 2088 01:25:40,639 --> 01:25:43,120 be responsible to have the same behavior 2089 01:25:43,120 --> 01:25:45,280 like we had previously so we should 2090 01:25:45,280 --> 01:25:47,120 still see 2 2091 01:25:47,120 --> 01:25:50,159 500 for this print line and we should 2092 01:25:50,159 --> 01:25:52,080 not see any errors and if i was to run 2093 01:25:52,080 --> 01:25:54,320 the program then you can see that we 2094 01:25:54,320 --> 01:25:57,199 receive the expected result so that way 2095 01:25:57,199 --> 01:25:58,960 we implement the best practices of 2096 01:25:58,960 --> 01:26:01,280 object-oriented programming for each 2097 01:26:01,280 --> 01:26:03,760 child class that we use a separated 2098 01:26:03,760 --> 01:26:06,480 constructor we also gonna need to call 2099 01:26:06,480 --> 01:26:09,199 the super function in order to have 2100 01:26:09,199 --> 01:26:10,639 fully access 2101 01:26:10,639 --> 01:26:13,199 for all the attributes and methods that 2102 01:26:13,199 --> 01:26:15,520 are coming from the class that we 2103 01:26:15,520 --> 01:26:17,600 inherit from all right so i minimized 2104 01:26:17,600 --> 01:26:20,480 the code for our classes and i also left 2105 01:26:20,480 --> 01:26:23,199 with one instance of phone here now i 2106 01:26:23,199 --> 01:26:24,960 want to show you the results of the 2107 01:26:24,960 --> 01:26:27,520 following things so i will say print and 2108 01:26:27,520 --> 01:26:31,040 i will see what is the list of all in 2109 01:26:31,040 --> 01:26:33,440 the item class is going to bring us back 2110 01:26:33,440 --> 01:26:35,840 so i'm going to say 2111 01:26:35,840 --> 01:26:38,639 and then i'm also going to say phone.all 2112 01:26:38,639 --> 01:26:40,239 if you remember we implemented this 2113 01:26:40,239 --> 01:26:43,040 class attributes as well here so i will 2114 01:26:43,040 --> 01:26:45,679 minimize the code back and then i will 2115 01:26:45,679 --> 01:26:47,679 run our program now you can see 2116 01:26:47,679 --> 01:26:51,040 something very weird in here we see item 2117 01:26:51,040 --> 01:26:52,880 and then we basically see the result of 2118 01:26:52,880 --> 01:26:55,760 the repr method that comes from the item 2119 01:26:55,760 --> 01:26:58,159 class now the reason that this happens 2120 01:26:58,159 --> 01:27:00,480 because we never implemented in our epr 2121 01:27:00,480 --> 01:27:03,199 method inside the phone class so that's 2122 01:27:03,199 --> 01:27:06,880 why we see this ungeneric result of item 2123 01:27:06,880 --> 01:27:09,280 now you can also pay attention that we 2124 01:27:09,280 --> 01:27:11,440 only create an instance of the phone 2125 01:27:11,440 --> 01:27:14,960 class so that's not so good that we see 2126 01:27:14,960 --> 01:27:18,080 item in those outputs so what we could 2127 01:27:18,080 --> 01:27:20,320 use instead of hard coding in the name 2128 01:27:20,320 --> 01:27:23,120 of the class in the repr method inside 2129 01:27:23,120 --> 01:27:26,080 the item class then we could access to 2130 01:27:26,080 --> 01:27:28,719 the name of the class generically now if 2131 01:27:28,719 --> 01:27:31,760 i was to replace this with some special 2132 01:27:31,760 --> 01:27:33,920 magic attribute that will be responsible 2133 01:27:33,920 --> 01:27:36,080 to give me the name of the class then 2134 01:27:36,080 --> 01:27:38,000 this will be perfect so i'm going to 2135 01:27:38,000 --> 01:27:40,719 delete that and i'm going to use curly 2136 01:27:40,719 --> 01:27:43,199 brackets and i'm going to say self 2137 01:27:43,199 --> 01:27:46,400 dot double underscore class 2138 01:27:46,400 --> 01:27:50,000 dot double underscore name so that is a 2139 01:27:50,000 --> 01:27:52,159 generic way to access to the name of the 2140 01:27:52,159 --> 01:27:55,120 class from the instance and by doing 2141 01:27:55,120 --> 01:27:58,000 this then besides receiving item hard 2142 01:27:58,000 --> 01:28:00,400 coded string then i should receive the 2143 01:28:00,400 --> 01:28:02,719 name of the class that i initialized 2144 01:28:02,719 --> 01:28:04,320 from the very beginning so this should 2145 01:28:04,320 --> 01:28:05,040 be 2146 01:28:05,040 --> 01:28:07,280 phone because that is the only single 2147 01:28:07,280 --> 01:28:09,120 instance that i have right now and you 2148 01:28:09,120 --> 01:28:10,960 can see that this is exactly the result 2149 01:28:10,960 --> 01:28:12,880 that i'm receiving back so that is 2150 01:28:12,880 --> 01:28:15,840 perfect now i said earlier that by using 2151 01:28:15,840 --> 01:28:18,639 the super function then we basically 2152 01:28:18,639 --> 01:28:20,800 have access to all the attributes and 2153 01:28:20,800 --> 01:28:22,719 the methods that are coming from the 2154 01:28:22,719 --> 01:28:25,120 class that we inherit from so what that 2155 01:28:25,120 --> 01:28:27,120 means it means that we will also have 2156 01:28:27,120 --> 01:28:30,080 the access to the class attribute of all 2157 01:28:30,080 --> 01:28:33,040 that is inside the item class and i'm 2158 01:28:33,040 --> 01:28:34,400 talking about 2159 01:28:34,400 --> 01:28:37,199 that attribute all right now to show you 2160 01:28:37,199 --> 01:28:39,679 that then i'm going to open back the 2161 01:28:39,679 --> 01:28:42,400 code from the phone class and i'm going 2162 01:28:42,400 --> 01:28:45,600 to remove the all attribute and i'm just 2163 01:28:45,600 --> 01:28:47,840 going to do that right now and i'm also 2164 01:28:47,840 --> 01:28:50,080 going to delete the actions to execute 2165 01:28:50,080 --> 01:28:53,040 where i use phone dot all dot append 2166 01:28:53,040 --> 01:28:55,280 because we no longer having the old 2167 01:28:55,280 --> 01:28:58,000 attribute in the phone class and if i 2168 01:28:58,000 --> 01:29:01,280 was to remove those and execute our 2169 01:29:01,280 --> 01:29:03,360 program now then you can see that i 2170 01:29:03,360 --> 01:29:05,920 still receive the same result so that is 2171 01:29:05,920 --> 01:29:08,800 a great idea removing the old attribute 2172 01:29:08,800 --> 01:29:11,520 in the child class level it is a great 2173 01:29:11,520 --> 01:29:14,400 idea to only use the old attribute in 2174 01:29:14,400 --> 01:29:16,639 the parent class because by using the 2175 01:29:16,639 --> 01:29:18,639 super function in the child class we 2176 01:29:18,639 --> 01:29:21,280 will have access to the old attribute so 2177 01:29:21,280 --> 01:29:23,520 this means that if one day would like to 2178 01:29:23,520 --> 01:29:26,480 have access to all the items instances 2179 01:29:26,480 --> 01:29:28,960 that have been initialized including the 2180 01:29:28,960 --> 01:29:31,520 child classes then accessing them from 2181 01:29:31,520 --> 01:29:34,480 item.all should also be enough now you 2182 01:29:34,480 --> 01:29:36,719 might be confused how this line is 2183 01:29:36,719 --> 01:29:39,360 responsible to add this instance inside 2184 01:29:39,360 --> 01:29:42,080 the all attribute that is happen to be a 2185 01:29:42,080 --> 01:29:44,880 list and that's happening because by 2186 01:29:44,880 --> 01:29:47,520 using the super function and as well as 2187 01:29:47,520 --> 01:29:50,880 the init then we basically call the init 2188 01:29:50,880 --> 01:29:53,760 method inside the parent class now in 2189 01:29:53,760 --> 01:29:55,840 the latest line inside this method we 2190 01:29:55,840 --> 01:29:58,159 also use item.old.append 2191 01:29:58,159 --> 01:29:59,920 which is also going to be accessible 2192 01:29:59,920 --> 01:30:02,560 from the phone class so that's why 2193 01:30:02,560 --> 01:30:04,080 calling the all 2194 01:30:04,080 --> 01:30:07,199 class attribute from the item class is a 2195 01:30:07,199 --> 01:30:09,360 better idea because it will give us the 2196 01:30:09,360 --> 01:30:12,239 complete picture okay so before diving 2197 01:30:12,239 --> 01:30:14,800 into the topic of that episode then 2198 01:30:14,800 --> 01:30:16,639 we're gonna need to do some code 2199 01:30:16,639 --> 01:30:18,560 organization in here because as you can 2200 01:30:18,560 --> 01:30:20,880 see for each of the child classes that 2201 01:30:20,880 --> 01:30:22,239 we will go ahead and create in the 2202 01:30:22,239 --> 01:30:24,480 future to extend this project then we're 2203 01:30:24,480 --> 01:30:26,560 going to need to do this in the main.py 2204 01:30:26,560 --> 01:30:28,800 file because that was the only single 2205 01:30:28,800 --> 01:30:30,960 file that we were working with and now 2206 01:30:30,960 --> 01:30:33,679 that our project grows we need to start 2207 01:30:33,679 --> 01:30:35,840 working with multiple files so that's 2208 01:30:35,840 --> 01:30:38,159 why maybe working with a file that will 2209 01:30:38,159 --> 01:30:40,239 represent the class of item and working 2210 01:30:40,239 --> 01:30:42,400 with a separate file that will represent 2211 01:30:42,400 --> 01:30:44,800 the phone child class will be a better 2212 01:30:44,800 --> 01:30:47,760 idea so we will have the main.py file 2213 01:30:47,760 --> 01:30:51,199 dedicated for only creating instances of 2214 01:30:51,199 --> 01:30:53,920 those classes so let's get started with 2215 01:30:53,920 --> 01:30:55,600 this so i'm going to go to the project 2216 01:30:55,600 --> 01:30:58,560 directory and create two python files 2217 01:30:58,560 --> 01:31:02,080 first one we will named item.py 2218 01:31:02,080 --> 01:31:05,840 the other one should be named phone.py 2219 01:31:05,840 --> 01:31:07,920 and i'm going to take the code from our 2220 01:31:07,920 --> 01:31:09,360 item class 2221 01:31:09,360 --> 01:31:13,440 and i'm just going to grab everything 2222 01:31:13,600 --> 01:31:16,000 like the following and i'm going to cut 2223 01:31:16,000 --> 01:31:19,199 this and then i'm going to paste this in 2224 01:31:19,199 --> 01:31:21,760 inside of that now pay attention that i 2225 01:31:21,760 --> 01:31:24,560 use the csv library so that's actually 2226 01:31:24,560 --> 01:31:26,639 the location that i need this library so 2227 01:31:26,639 --> 01:31:28,080 i'm going to 2228 01:31:28,080 --> 01:31:30,560 just copy the import line and that 2229 01:31:30,560 --> 01:31:32,800 should be good enough now i'm going to 2230 01:31:32,800 --> 01:31:35,679 do the same process for the 2231 01:31:35,679 --> 01:31:38,320 phone.py and i'm going to be copying 2232 01:31:38,320 --> 01:31:41,280 this into the phone.py file as well but 2233 01:31:41,280 --> 01:31:44,480 now this file needs to import the item 2234 01:31:44,480 --> 01:31:46,239 class because as you can see we got an 2235 01:31:46,239 --> 01:31:50,080 error here so we should say from item 2236 01:31:50,080 --> 01:31:52,719 import item like the following and the 2237 01:31:52,719 --> 01:31:54,960 errors should be gone and then in the 2238 01:31:54,960 --> 01:31:57,920 main.py file we can basically use this 2239 01:31:57,920 --> 01:32:00,480 file to only instantiate instances 2240 01:32:00,480 --> 01:32:02,960 meaning creating data that will 2241 01:32:02,960 --> 01:32:05,120 represent something to python so this 2242 01:32:05,120 --> 01:32:06,960 means that we can go ahead and import 2243 01:32:06,960 --> 01:32:09,360 the class from the item file 2244 01:32:09,360 --> 01:32:11,440 we can do the same 2245 01:32:11,440 --> 01:32:12,960 from the 2246 01:32:12,960 --> 01:32:15,199 phone file and then we can go ahead and 2247 01:32:15,199 --> 01:32:17,280 do the stuff that we used to do so we 2248 01:32:17,280 --> 01:32:18,960 can say item dot 2249 01:32:18,960 --> 01:32:22,080 install sheet from csv and to verify 2250 01:32:22,080 --> 01:32:26,000 that this works we can also say print 2251 01:32:26,000 --> 01:32:28,320 item dot all like that 2252 01:32:28,320 --> 01:32:30,400 and if we want to run this file now to 2253 01:32:30,400 --> 01:32:32,480 see that this works then we can do that 2254 01:32:32,480 --> 01:32:34,000 and you can see that everything works 2255 01:32:34,000 --> 01:32:36,560 just as expected now just a quick side 2256 01:32:36,560 --> 01:32:38,480 note i'm not going to rely too much on 2257 01:32:38,480 --> 01:32:40,400 the child class that we have created in 2258 01:32:40,400 --> 01:32:42,800 the latest episode to show the problems 2259 01:32:42,800 --> 01:32:44,080 that we're going to solve in that 2260 01:32:44,080 --> 01:32:46,000 episode i'm going to rely 2261 01:32:46,000 --> 01:32:49,120 more on the item class so that it will 2262 01:32:49,120 --> 01:32:50,960 be easier to follow and we will not 2263 01:32:50,960 --> 01:32:53,440 complex things too much now that doesn't 2264 01:32:53,440 --> 01:32:55,840 mean that i do not recommend using child 2265 01:32:55,840 --> 01:32:57,679 classes or something like that but it 2266 01:32:57,679 --> 01:32:59,840 will be just easier to show you the 2267 01:32:59,840 --> 01:33:01,840 cases that i'm going to show in the 2268 01:33:01,840 --> 01:33:04,480 parent class so that's why for example i 2269 01:33:04,480 --> 01:33:07,199 deleted temporarily the import line of 2270 01:33:07,199 --> 01:33:10,159 the phone class and i just came up with 2271 01:33:10,159 --> 01:33:13,040 a random item instance that name is my 2272 01:33:13,040 --> 01:33:15,120 item and the price happened to be that 2273 01:33:15,120 --> 01:33:16,800 number i did not specify quantity 2274 01:33:16,800 --> 01:33:19,040 because we have a default value and now 2275 01:33:19,040 --> 01:33:20,880 after this line you can see that i 2276 01:33:20,880 --> 01:33:24,080 override this attribute by the string of 2277 01:33:24,080 --> 01:33:26,560 other item now the expected result is 2278 01:33:26,560 --> 01:33:28,800 not going to surprise anyone because we 2279 01:33:28,800 --> 01:33:30,719 see at the right time when we print this 2280 01:33:30,719 --> 01:33:32,719 attribute but we 2281 01:33:32,719 --> 01:33:35,199 might want to ask ourselves is that a 2282 01:33:35,199 --> 01:33:37,920 behavior that we always want what if we 2283 01:33:37,920 --> 01:33:40,639 want to restrict our users to change the 2284 01:33:40,639 --> 01:33:43,520 attribute of name once the name has been 2285 01:33:43,520 --> 01:33:45,840 set up in the initialization meaning in 2286 01:33:45,840 --> 01:33:48,320 that line well that's something that we 2287 01:33:48,320 --> 01:33:50,880 might want to achieve for critical 2288 01:33:50,880 --> 01:33:53,040 attributes like the name of your 2289 01:33:53,040 --> 01:33:55,120 instances and in our case 2290 01:33:55,120 --> 01:33:58,000 the name of our item so what we could do 2291 01:33:58,000 --> 01:34:00,000 we could actually go ahead and create 2292 01:34:00,000 --> 01:34:02,960 read-only so-called attributes meaning 2293 01:34:02,960 --> 01:34:06,400 that we have only one opportunity to set 2294 01:34:06,400 --> 01:34:09,440 the name of our item and then we cannot 2295 01:34:09,440 --> 01:34:11,920 touch the value of that anymore so what 2296 01:34:11,920 --> 01:34:13,760 that means it means that we can set up 2297 01:34:13,760 --> 01:34:16,560 this in the initialization and we should 2298 01:34:16,560 --> 01:34:18,800 have errors if we try to override the 2299 01:34:18,800 --> 01:34:21,840 value of that now that's also known as 2300 01:34:21,840 --> 01:34:24,719 encapsulation when we talk about the 2301 01:34:24,719 --> 01:34:26,400 principles of object-oriented 2302 01:34:26,400 --> 01:34:28,880 programming which i will be focusing 2303 01:34:28,880 --> 01:34:31,440 more on the future episodes but now 2304 01:34:31,440 --> 01:34:33,199 let's go ahead and see how we can come 2305 01:34:33,199 --> 01:34:35,760 up with read-only attributes how we can 2306 01:34:35,760 --> 01:34:38,400 restrict our users to override the 2307 01:34:38,400 --> 01:34:41,119 attributes after the initialization of 2308 01:34:41,119 --> 01:34:42,800 our instances 2309 01:34:42,800 --> 01:34:44,400 okay so on the left side we have the 2310 01:34:44,400 --> 01:34:46,800 main.py file and on the right side we 2311 01:34:46,800 --> 01:34:48,960 have the item.py file which we are going 2312 01:34:48,960 --> 01:34:51,199 to work on and inside the class i'm 2313 01:34:51,199 --> 01:34:53,679 going to create our first read-only 2314 01:34:53,679 --> 01:34:55,920 attribute now the way that you can start 2315 01:34:55,920 --> 01:34:59,119 doing this is by first using a decorator 2316 01:34:59,119 --> 01:35:00,560 and if you remember from the previous 2317 01:35:00,560 --> 01:35:02,880 episodes decorators are like functions 2318 01:35:02,880 --> 01:35:04,960 that you can pre-execute before another 2319 01:35:04,960 --> 01:35:07,920 function so i could go ahead and use the 2320 01:35:07,920 --> 01:35:12,320 property decorator and then go ahead and 2321 01:35:12,320 --> 01:35:14,639 create a function and here's the exact 2322 01:35:14,639 --> 01:35:17,600 location that i could set up the name of 2323 01:35:17,600 --> 01:35:20,880 our read-only attribute so for testing 2324 01:35:20,880 --> 01:35:22,960 reasons let's go ahead and call this 2325 01:35:22,960 --> 01:35:25,520 read-only name something in that kind 2326 01:35:25,520 --> 01:35:27,679 all right and then i will open up and 2327 01:35:27,679 --> 01:35:29,679 close parentheses and this will 2328 01:35:29,679 --> 01:35:31,520 obviously receive self because it's 2329 01:35:31,520 --> 01:35:33,280 going to be belong to each of the 2330 01:35:33,280 --> 01:35:36,400 instances and now for testing purposes 2331 01:35:36,400 --> 01:35:38,239 let's only go ahead and return a random 2332 01:35:38,239 --> 01:35:39,840 string like 2333 01:35:39,840 --> 01:35:42,800 a three times all right and then now 2334 01:35:42,800 --> 01:35:45,199 that we have done this i can go to our 2335 01:35:45,199 --> 01:35:48,400 main.py file and try to access this 2336 01:35:48,400 --> 01:35:50,320 property now pay attention that i'm 2337 01:35:50,320 --> 01:35:53,280 going to call those properties and not 2338 01:35:53,280 --> 01:35:56,159 attributes so i'm going to go here and 2339 01:35:56,159 --> 01:35:57,760 i'm going to try to 2340 01:35:57,760 --> 01:36:00,639 print item1 dot 2341 01:36:00,639 --> 01:36:03,280 name and now that i have wrote name pay 2342 01:36:03,280 --> 01:36:05,040 attention to the differences in this 2343 01:36:05,040 --> 01:36:08,000 drop down for read only name we receive 2344 01:36:08,000 --> 01:36:10,000 a totally different icon here on the 2345 01:36:10,000 --> 01:36:12,880 left side which stands for a property 2346 01:36:12,880 --> 01:36:15,440 where in here we see the f letter which 2347 01:36:15,440 --> 01:36:18,000 stands for a regular field so if i was 2348 01:36:18,000 --> 01:36:21,199 to try to print that and run our program 2349 01:36:21,199 --> 01:36:22,880 then obviously we will receive the 2350 01:36:22,880 --> 01:36:26,239 expected result but if i was to try to 2351 01:36:26,239 --> 01:36:29,679 set a new value for the read-only name 2352 01:36:29,679 --> 01:36:31,199 say that we want to change this to 2353 01:36:31,199 --> 01:36:32,960 something like that then you're gonna 2354 01:36:32,960 --> 01:36:34,880 see that pycharm is going to complain 2355 01:36:34,880 --> 01:36:37,440 about this and even if we try to execute 2356 01:36:37,440 --> 01:36:39,280 that then we will end up with an 2357 01:36:39,280 --> 01:36:41,760 exception that says attribute error 2358 01:36:41,760 --> 01:36:44,800 can't set attribute so that is how 2359 01:36:44,800 --> 01:36:47,119 read-only attributes so-called are 2360 01:36:47,119 --> 01:36:49,600 working in python you can create those 2361 01:36:49,600 --> 01:36:52,400 by using a property decorator before 2362 01:36:52,400 --> 01:36:55,040 your functions and return whatever value 2363 01:36:55,040 --> 01:36:57,600 you would like to return now the biggest 2364 01:36:57,600 --> 01:37:00,560 challenge here is going to be converting 2365 01:37:00,560 --> 01:37:03,920 the name attribute that we actually have 2366 01:37:03,920 --> 01:37:07,440 which is happen to be exactly here into 2367 01:37:07,440 --> 01:37:09,920 being a read-only attribute and that is 2368 01:37:09,920 --> 01:37:11,840 going to be a little bit challenging but 2369 01:37:11,840 --> 01:37:14,320 let's go ahead and start working on that 2370 01:37:14,320 --> 01:37:16,159 so first things first i'm going to 2371 01:37:16,159 --> 01:37:18,159 delete those three lines because we are 2372 01:37:18,159 --> 01:37:20,320 not going to use this property anymore 2373 01:37:20,320 --> 01:37:23,199 and i'm going to scroll up a bit and 2374 01:37:23,199 --> 01:37:26,800 work underneath this constructor here so 2375 01:37:26,800 --> 01:37:29,760 you might think that converting the name 2376 01:37:29,760 --> 01:37:31,920 attribute into being read only meaning a 2377 01:37:31,920 --> 01:37:34,639 property is as easy as doing something 2378 01:37:34,639 --> 01:37:35,600 like 2379 01:37:35,600 --> 01:37:37,840 first using the property decorator and 2380 01:37:37,840 --> 01:37:40,320 then go ahead and say def name 2381 01:37:40,320 --> 01:37:42,880 then receive self as the parameter and 2382 01:37:42,880 --> 01:37:45,440 then use something like return self dot 2383 01:37:45,440 --> 01:37:47,199 name because we already have the self 2384 01:37:47,199 --> 01:37:50,239 type name assigned to the self object 2385 01:37:50,239 --> 01:37:52,480 but actually doing something like this 2386 01:37:52,480 --> 01:37:55,119 is like saying to that class hey from 2387 01:37:55,119 --> 01:37:58,000 now on you are going to have a name 2388 01:37:58,000 --> 01:38:01,360 attribute that is going to be read only 2389 01:38:01,360 --> 01:38:03,840 and that is straightforward the effect 2390 01:38:03,840 --> 01:38:05,920 of the property decorator so i'm going 2391 01:38:05,920 --> 01:38:07,760 to leave a comment here that is going to 2392 01:38:07,760 --> 01:38:09,679 look like the following but if you 2393 01:38:09,679 --> 01:38:10,719 remember 2394 01:38:10,719 --> 01:38:14,960 we try to set the self.name into a new 2395 01:38:14,960 --> 01:38:19,040 value inside our constructor so you can 2396 01:38:19,040 --> 01:38:22,639 see that this action is illegal because 2397 01:38:22,639 --> 01:38:25,679 we have a read only property here so 2398 01:38:25,679 --> 01:38:27,840 when you go ahead and create a property 2399 01:38:27,840 --> 01:38:30,960 with the name of basically name then you 2400 01:38:30,960 --> 01:38:33,920 are no more allowed to set this 2401 01:38:33,920 --> 01:38:36,719 value anymore you are only allowed to 2402 01:38:36,719 --> 01:38:39,679 have access to see this back in whatever 2403 01:38:39,679 --> 01:38:42,080 instance you will create so that is why 2404 01:38:42,080 --> 01:38:44,159 if i was to hover my mouse here then 2405 01:38:44,159 --> 01:38:45,920 we're gonna see an error that is saying 2406 01:38:45,920 --> 01:38:48,880 property name cannot be set so the 2407 01:38:48,880 --> 01:38:51,840 pythonic way to doing this workaround to 2408 01:38:51,840 --> 01:38:53,440 get over this 2409 01:38:53,440 --> 01:38:56,400 is using one underscore before the name 2410 01:38:56,400 --> 01:38:59,119 of our actual attribute name that we 2411 01:38:59,119 --> 01:39:02,400 assign to the self object and by doing 2412 01:39:02,400 --> 01:39:05,040 this we earn a couple of things that are 2413 01:39:05,040 --> 01:39:07,760 quite important so first let me add here 2414 01:39:07,760 --> 01:39:10,159 an underscore and just use something 2415 01:39:10,159 --> 01:39:13,360 like that and then now i need to go to 2416 01:39:13,360 --> 01:39:15,679 my property function meaning the 2417 01:39:15,679 --> 01:39:18,000 property attribute and i'm gonna need to 2418 01:39:18,000 --> 01:39:20,159 add here the double underscore as well 2419 01:39:20,159 --> 01:39:22,080 because first things first i go ahead 2420 01:39:22,080 --> 01:39:24,400 and set up the value for my double 2421 01:39:24,400 --> 01:39:26,719 underscore excuse me single underscore 2422 01:39:26,719 --> 01:39:29,360 name into being equal to the 2423 01:39:29,360 --> 01:39:31,600 value of this parameter here and then i 2424 01:39:31,600 --> 01:39:33,520 go ahead and use one more read only 2425 01:39:33,520 --> 01:39:35,920 attribute that i intentionally give the 2426 01:39:35,920 --> 01:39:38,639 name of name and i and then i return 2427 01:39:38,639 --> 01:39:41,840 self dot underscore name now i can go 2428 01:39:41,840 --> 01:39:45,040 back to my main.py file and see what 2429 01:39:45,040 --> 01:39:47,360 effects those lines are having right now 2430 01:39:47,360 --> 01:39:50,480 on our instances so first i can go ahead 2431 01:39:50,480 --> 01:39:53,760 and set a name for my item and i can 2432 01:39:53,760 --> 01:39:57,440 access to the name of this item 2433 01:39:57,440 --> 01:39:59,760 by saying something like item one dot 2434 01:39:59,760 --> 01:40:02,000 name so i don't really have to go ahead 2435 01:40:02,000 --> 01:40:03,840 and use item one dot underscore name 2436 01:40:03,840 --> 01:40:05,520 because that is going to be a little bit 2437 01:40:05,520 --> 01:40:08,639 ugly and not convenient because 2438 01:40:08,639 --> 01:40:10,639 accessing attributes with always an 2439 01:40:10,639 --> 01:40:13,199 underscore before is not nice for each 2440 01:40:13,199 --> 01:40:15,280 of the instances that you look to access 2441 01:40:15,280 --> 01:40:17,760 to their attributes doing this one time 2442 01:40:17,760 --> 01:40:19,520 inside the class is going to make it 2443 01:40:19,520 --> 01:40:22,000 okay but trying to access those 2444 01:40:22,000 --> 01:40:24,480 attributes outside of your class meaning 2445 01:40:24,480 --> 01:40:26,719 from the instances is not going to turn 2446 01:40:26,719 --> 01:40:28,880 it into too much pretty so that is the 2447 01:40:28,880 --> 01:40:31,440 best way to overcome such a thing and 2448 01:40:31,440 --> 01:40:34,639 now if i was to try to print that 2449 01:40:34,639 --> 01:40:35,440 then 2450 01:40:35,440 --> 01:40:38,080 excuse me let me fix that quickly by 2451 01:40:38,080 --> 01:40:40,159 item1 dot name 2452 01:40:40,159 --> 01:40:42,000 and run our program then you can see 2453 01:40:42,000 --> 01:40:44,560 that that is working and now let's go 2454 01:40:44,560 --> 01:40:47,280 ahead and also see if we can set our 2455 01:40:47,280 --> 01:40:50,719 name into being equal to another 2456 01:40:50,719 --> 01:40:52,560 thing like that 2457 01:40:52,560 --> 01:40:54,480 see if that works you can see that i 2458 01:40:54,480 --> 01:40:58,080 cannot set that attribute but however i 2459 01:40:58,080 --> 01:41:01,679 can still see this underscore name from 2460 01:41:01,679 --> 01:41:04,560 the instance level and that is maybe 2461 01:41:04,560 --> 01:41:06,719 something that you look to avoid it 2462 01:41:06,719 --> 01:41:09,679 could have been a lot nicer if we could 2463 01:41:09,679 --> 01:41:12,880 somehow prevent totally the access from 2464 01:41:12,880 --> 01:41:15,840 this underscore name in here so the way 2465 01:41:15,840 --> 01:41:18,800 that this is achievable is by adding one 2466 01:41:18,800 --> 01:41:21,119 more underscore to the attribute name 2467 01:41:21,119 --> 01:41:23,360 now this might remind you something that 2468 01:41:23,360 --> 01:41:26,320 is called private attribute if you are 2469 01:41:26,320 --> 01:41:28,480 familiar with programming languages like 2470 01:41:28,480 --> 01:41:31,440 java or c-sharp that is pretty much the 2471 01:41:31,440 --> 01:41:33,520 same behavior of using the private 2472 01:41:33,520 --> 01:41:36,400 keyword before your attributes in those 2473 01:41:36,400 --> 01:41:38,639 kind of programming languages where it 2474 01:41:38,639 --> 01:41:40,639 has different principles when it comes 2475 01:41:40,639 --> 01:41:42,719 to object-oriented programming so to sum 2476 01:41:42,719 --> 01:41:44,719 up if you add one more underscore to 2477 01:41:44,719 --> 01:41:46,480 your attribute names meaning you use 2478 01:41:46,480 --> 01:41:48,480 double underscore then you basically 2479 01:41:48,480 --> 01:41:51,679 prevent the access to those attributes 2480 01:41:51,679 --> 01:41:55,040 totally outside of the class so let's 2481 01:41:55,040 --> 01:41:56,800 see a simulation of that so i'm going to 2482 01:41:56,800 --> 01:41:58,800 minimize the terminal and i'm going to 2483 01:41:58,800 --> 01:42:02,400 go to my item.py file and besides using 2484 01:42:02,400 --> 01:42:05,119 here single underscore i'm going to add 2485 01:42:05,119 --> 01:42:07,119 one more and then i'm going to change 2486 01:42:07,119 --> 01:42:09,280 this to double underscore as well and 2487 01:42:09,280 --> 01:42:13,600 now if we were to go to our main.py file 2488 01:42:13,600 --> 01:42:14,400 and 2489 01:42:14,400 --> 01:42:16,880 let's use here item one 2490 01:42:16,880 --> 01:42:19,520 dot and try to basically use 2491 01:42:19,520 --> 01:42:21,360 double underscore and try to access to 2492 01:42:21,360 --> 01:42:23,119 name now you can see that i don't even 2493 01:42:23,119 --> 01:42:24,960 have an auto completion from my drop 2494 01:42:24,960 --> 01:42:27,920 down because i don't have access to see 2495 01:42:27,920 --> 01:42:30,320 this attribute from the instance level 2496 01:42:30,320 --> 01:42:32,320 and that is something that you look to 2497 01:42:32,320 --> 01:42:34,560 achieve when you want to have a clean 2498 01:42:34,560 --> 01:42:36,719 read-only attribute and that is the way 2499 01:42:36,719 --> 01:42:39,119 that you can do that so if i was to try 2500 01:42:39,119 --> 01:42:41,840 to print this then that's just going to 2501 01:42:41,840 --> 01:42:44,239 complain about how it does not have the 2502 01:42:44,239 --> 01:42:46,800 attribute of double underscore name 2503 01:42:46,800 --> 01:42:48,400 in this instance 2504 01:42:48,400 --> 01:42:50,080 and again if i was to remove those 2505 01:42:50,080 --> 01:42:52,639 double underscores then i will just 2506 01:42:52,639 --> 01:42:55,360 access it as a property meaning as a 2507 01:42:55,360 --> 01:42:57,679 read-only attribute and that is exactly 2508 01:42:57,679 --> 01:43:00,000 what i look to have here all right so 2509 01:43:00,000 --> 01:43:02,639 now that we got the idea of that then we 2510 01:43:02,639 --> 01:43:05,119 still might be curious about how to 2511 01:43:05,119 --> 01:43:07,840 set a new value for the name attribute 2512 01:43:07,840 --> 01:43:09,760 now obviously using the property 2513 01:43:09,760 --> 01:43:12,239 decorator is going to turn this into 2514 01:43:12,239 --> 01:43:14,239 being a read-only attribute but there 2515 01:43:14,239 --> 01:43:15,920 are still some decorators that will 2516 01:43:15,920 --> 01:43:18,800 allow you to however set a new value for 2517 01:43:18,800 --> 01:43:19,760 this 2518 01:43:19,760 --> 01:43:22,400 property of name so let's see how that 2519 01:43:22,400 --> 01:43:24,239 is achievable so obviously that is not 2520 01:43:24,239 --> 01:43:26,880 going to work because it says 2521 01:43:26,880 --> 01:43:30,320 can't set attribute so what we can do is 2522 01:43:30,320 --> 01:43:32,800 we can use a new method where we can 2523 01:43:32,800 --> 01:43:35,920 declare that we'd like to also set a new 2524 01:43:35,920 --> 01:43:40,159 value for this attribute that we named 2525 01:43:40,159 --> 01:43:40,960 name 2526 01:43:40,960 --> 01:43:43,520 so the way that that's going to work is 2527 01:43:43,520 --> 01:43:46,320 by going to our class here and using 2528 01:43:46,320 --> 01:43:48,639 here one more method with a special 2529 01:43:48,639 --> 01:43:51,199 decorator now this decorator is going to 2530 01:43:51,199 --> 01:43:52,960 look like the following so i'm going to 2531 01:43:52,960 --> 01:43:54,639 refer to the 2532 01:43:54,639 --> 01:43:57,199 name because that's the property name 2533 01:43:57,199 --> 01:44:00,159 and then i'm going to use the dot setter 2534 01:44:00,159 --> 01:44:03,520 so by doing this then i basically say 2535 01:44:03,520 --> 01:44:06,239 hey so i still want to set a new value 2536 01:44:06,239 --> 01:44:08,400 for that name although 2537 01:44:08,400 --> 01:44:11,280 that is a property meaning a read-only 2538 01:44:11,280 --> 01:44:13,679 attribute so now if i was to 2539 01:44:13,679 --> 01:44:16,880 go down and say something like def name 2540 01:44:16,880 --> 01:44:19,760 and this would receive self and as well 2541 01:44:19,760 --> 01:44:21,679 as one additional parameter because the 2542 01:44:21,679 --> 01:44:23,520 additional parameter should refer to the 2543 01:44:23,520 --> 01:44:25,679 new value that i want to set to that 2544 01:44:25,679 --> 01:44:28,000 name so i will receive a parameter that 2545 01:44:28,000 --> 01:44:30,400 i could name something like value and 2546 01:44:30,400 --> 01:44:33,360 then inside here i'm only going to set 2547 01:44:33,360 --> 01:44:36,159 the new value for our double underscore 2548 01:44:36,159 --> 01:44:38,320 name because if you remember 2549 01:44:38,320 --> 01:44:41,280 when an instance tries to see the value 2550 01:44:41,280 --> 01:44:44,360 of name then we basically return 2551 01:44:44,360 --> 01:44:47,360 self.double underscore name so when a 2552 01:44:47,360 --> 01:44:51,040 user will try to set the name again to a 2553 01:44:51,040 --> 01:44:54,159 new value then it should execute self 2554 01:44:54,159 --> 01:44:55,040 dot 2555 01:44:55,040 --> 01:44:59,360 name equals to value and by doing this i 2556 01:44:59,360 --> 01:45:02,239 basically allow our users to yet set a 2557 01:45:02,239 --> 01:45:04,080 new value for 2558 01:45:04,080 --> 01:45:04,880 name 2559 01:45:04,880 --> 01:45:06,880 so now let's show what effect those 2560 01:45:06,880 --> 01:45:08,719 three lines are going to have in our 2561 01:45:08,719 --> 01:45:11,119 main dot py you can see that now the 2562 01:45:11,119 --> 01:45:15,040 error is gone i can now go down here and 2563 01:45:15,040 --> 01:45:18,000 use print item one dot name 2564 01:45:18,000 --> 01:45:20,080 and that's going to work you can see 2565 01:45:20,080 --> 01:45:23,440 that i have other item so this means not 2566 01:45:23,440 --> 01:45:27,199 only i can set a new value for my 2567 01:45:27,199 --> 01:45:29,199 underscore name so-called 2568 01:45:29,199 --> 01:45:32,000 in the initialization i can also do that 2569 01:45:32,000 --> 01:45:35,520 later on if i only use this convention 2570 01:45:35,520 --> 01:45:38,880 in here now those getters and said this 2571 01:45:38,880 --> 01:45:41,040 thing are always confusing in no matter 2572 01:45:41,040 --> 01:45:43,119 programming language you work so i will 2573 01:45:43,119 --> 01:45:45,679 do a final summary of all what we have 2574 01:45:45,679 --> 01:45:48,400 learned until this point all right so 2575 01:45:48,400 --> 01:45:50,880 using add property 2576 01:45:50,880 --> 01:45:53,119 will basically give you a control of 2577 01:45:53,119 --> 01:45:56,000 what you'd like to do when you get an 2578 01:45:56,000 --> 01:45:58,639 attribute and also by using this then 2579 01:45:58,639 --> 01:46:00,960 you basically convert this attribute 2580 01:46:00,960 --> 01:46:03,600 into being read only if i was not 2581 01:46:03,600 --> 01:46:04,719 implemented 2582 01:46:04,719 --> 01:46:06,880 these setters in 2583 01:46:06,880 --> 01:46:09,840 here so you can see that now when i 2584 01:46:09,840 --> 01:46:12,480 commented those out then this line is 2585 01:46:12,480 --> 01:46:15,040 going to have some problems because by 2586 01:46:15,040 --> 01:46:17,119 not doing this then i basically say that 2587 01:46:17,119 --> 01:46:19,520 hey name is read only you cannot set 2588 01:46:19,520 --> 01:46:21,840 that if i want to again uncomment those 2589 01:46:21,840 --> 01:46:24,320 back then i will have the control to set 2590 01:46:24,320 --> 01:46:26,480 this attribute to whatever attribute i'd 2591 01:46:26,480 --> 01:46:29,440 like to now by using 2592 01:46:29,440 --> 01:46:31,840 this statement here basically getting 2593 01:46:31,840 --> 01:46:34,719 the attribute then i basically call the 2594 01:46:34,719 --> 01:46:37,520 bunch of codes that are being executed 2595 01:46:37,520 --> 01:46:41,679 in here so whenever i use item1.name 2596 01:46:41,679 --> 01:46:43,840 then python says to itself okay you try 2597 01:46:43,840 --> 01:46:46,719 to get that attribute so i will go ahead 2598 01:46:46,719 --> 01:46:48,560 and try to execute all the lines of 2599 01:46:48,560 --> 01:46:50,320 codes are 2600 01:46:50,320 --> 01:46:53,040 that are here so that is what exactly 2601 01:46:53,040 --> 01:46:55,199 happening here and to show you that then 2602 01:46:55,199 --> 01:46:58,320 i can just use a random print function 2603 01:46:58,320 --> 01:47:00,080 here that will say 2604 01:47:00,080 --> 01:47:02,320 you are trying to 2605 01:47:02,320 --> 01:47:07,199 get name like that then you should see 2606 01:47:07,199 --> 01:47:09,600 this line being printed right before 2607 01:47:09,600 --> 01:47:12,000 what the actual value is because at 2608 01:47:12,000 --> 01:47:12,880 first 2609 01:47:12,880 --> 01:47:15,119 we print you are trying to print the 2610 01:47:15,119 --> 01:47:18,000 name and then we return the self dot 2611 01:47:18,000 --> 01:47:20,159 underscore name so it prints that over 2612 01:47:20,159 --> 01:47:22,800 here so that is what happening when you 2613 01:47:22,800 --> 01:47:23,920 try to 2614 01:47:23,920 --> 01:47:27,280 get an attribute but when you try to set 2615 01:47:27,280 --> 01:47:30,239 an attribute then python says to itself 2616 01:47:30,239 --> 01:47:33,360 okay so here you try to set an attribute 2617 01:47:33,360 --> 01:47:35,600 so because you set an attribute then i 2618 01:47:35,600 --> 01:47:37,520 should go ahead and 2619 01:47:37,520 --> 01:47:40,480 execute the code that is inside here 2620 01:47:40,480 --> 01:47:43,280 because that is the setter of that 2621 01:47:43,280 --> 01:47:45,760 attribute so that is why when you go 2622 01:47:45,760 --> 01:47:46,960 ahead and 2623 01:47:46,960 --> 01:47:48,880 use this decorator then you should 2624 01:47:48,880 --> 01:47:52,159 always receive a parameter because the 2625 01:47:52,159 --> 01:47:55,280 other item is going to be passed as an 2626 01:47:55,280 --> 01:47:58,000 argument to that parameter it is very 2627 01:47:58,000 --> 01:48:00,159 important to understand that and that is 2628 01:48:00,159 --> 01:48:02,960 why i can only allow myself to use one 2629 01:48:02,960 --> 01:48:05,920 line of code that will say self dot 2630 01:48:05,920 --> 01:48:07,600 double underscore name 2631 01:48:07,600 --> 01:48:10,719 is equal to the new value that you try 2632 01:48:10,719 --> 01:48:11,920 to 2633 01:48:11,920 --> 01:48:14,960 set and to show you that again i can go 2634 01:48:14,960 --> 01:48:17,520 here and say print 2635 01:48:17,520 --> 01:48:19,520 you are trying 2636 01:48:19,520 --> 01:48:22,560 to set and this line should appear just 2637 01:48:22,560 --> 01:48:24,880 before this print line because 2638 01:48:24,880 --> 01:48:26,880 at first i try to set 2639 01:48:26,880 --> 01:48:28,960 a different value for name and then i 2640 01:48:28,960 --> 01:48:31,840 just print it back like that okay so if 2641 01:48:31,840 --> 01:48:33,920 i was to run that then you can see that 2642 01:48:33,920 --> 01:48:35,920 at first we see the line of you are 2643 01:48:35,920 --> 01:48:38,560 trying to set then right after it we 2644 01:48:38,560 --> 01:48:41,440 actually see whatever item one dot name 2645 01:48:41,440 --> 01:48:43,920 is equal to now the reason that the 2646 01:48:43,920 --> 01:48:47,360 value is set it is because we set it 2647 01:48:47,360 --> 01:48:49,360 over here and then the next time i try 2648 01:48:49,360 --> 01:48:51,600 to get the value then those lines are 2649 01:48:51,600 --> 01:48:53,679 getting executed so that is the life 2650 01:48:53,679 --> 01:48:55,760 cycle of getters and setters and that is 2651 01:48:55,760 --> 01:48:58,080 the way that it works by having the 2652 01:48:58,080 --> 01:49:00,080 control of whatever you'd like to do 2653 01:49:00,080 --> 01:49:02,719 when you set a new value you can also 2654 01:49:02,719 --> 01:49:05,920 restrict it you can go ahead and do some 2655 01:49:05,920 --> 01:49:07,760 conditioning or you can go ahead and 2656 01:49:07,760 --> 01:49:10,080 raise some exceptions if you don't like 2657 01:49:10,080 --> 01:49:12,320 the value that you receive let's say 2658 01:49:12,320 --> 01:49:14,800 that you want to restrict the length of 2659 01:49:14,800 --> 01:49:17,040 the characters for the name of that 2660 01:49:17,040 --> 01:49:19,119 attribute all right so that is something 2661 01:49:19,119 --> 01:49:21,679 that you can do you can actually go here 2662 01:49:21,679 --> 01:49:23,199 and say if 2663 01:49:23,199 --> 01:49:24,560 len 2664 01:49:24,560 --> 01:49:26,159 of the value 2665 01:49:26,159 --> 01:49:30,000 is um greater than 10 2666 01:49:30,000 --> 01:49:32,719 then you would like to raise exception 2667 01:49:32,719 --> 01:49:34,320 that will say 2668 01:49:34,320 --> 01:49:36,719 something like 2669 01:49:36,719 --> 01:49:38,320 your 2670 01:49:38,320 --> 01:49:42,239 the name is too long something like that 2671 01:49:42,239 --> 01:49:43,840 and then you can say 2672 01:49:43,840 --> 01:49:46,080 else and then you can execute the line 2673 01:49:46,080 --> 01:49:47,920 that will be responsible to set that 2674 01:49:47,920 --> 01:49:49,840 value so intentionally i'm going to 2675 01:49:49,840 --> 01:49:51,599 leave it as it is because the length of 2676 01:49:51,599 --> 01:49:52,800 that is 2677 01:49:52,800 --> 01:49:54,880 nine characters so we should not have 2678 01:49:54,880 --> 01:49:58,960 any arrows but if i was to add here 2679 01:49:58,960 --> 01:50:01,440 two more characters like that and 2680 01:50:01,440 --> 01:50:03,520 execute it then you can see that we are 2681 01:50:03,520 --> 01:50:05,360 going to receive an exception that will 2682 01:50:05,360 --> 01:50:08,320 say the name is too long so that's how 2683 01:50:08,320 --> 01:50:10,080 ghettos and setters are working in 2684 01:50:10,080 --> 01:50:12,480 python you now have all the knowledge 2685 01:50:12,480 --> 01:50:14,320 that you need to play around with 2686 01:50:14,320 --> 01:50:16,719 different attributes and manage them the 2687 01:50:16,719 --> 01:50:18,960 way that you like to so i believe that 2688 01:50:18,960 --> 01:50:20,880 after the information that you received 2689 01:50:20,880 --> 01:50:24,080 in that episode you have everything that 2690 01:50:24,080 --> 01:50:25,840 you need to manage your attitude 2691 01:50:25,840 --> 01:50:27,760 successfully and play around with it and 2692 01:50:27,760 --> 01:50:30,080 as well as coming up with rich classes 2693 01:50:30,080 --> 01:50:32,080 that will have multiple attributes and 2694 01:50:32,080 --> 01:50:34,239 then you can set up special behaviors to 2695 01:50:34,239 --> 01:50:36,480 those attributes and also you can decide 2696 01:50:36,480 --> 01:50:39,599 that you'll not like to force to receive 2697 01:50:39,599 --> 01:50:41,440 those attributes in the constructor you 2698 01:50:41,440 --> 01:50:43,520 can decide that you can delete some 2699 01:50:43,520 --> 01:50:46,000 parameters in your constructor and you 2700 01:50:46,000 --> 01:50:48,880 can say that you will not like to assign 2701 01:50:48,880 --> 01:50:51,119 those to the self object immediately 2702 01:50:51,119 --> 01:50:53,440 when you create an instance so whatever 2703 01:50:53,440 --> 01:50:55,199 you would like to you have all the tools 2704 01:50:55,199 --> 01:50:58,159 to play around with how to manage your 2705 01:50:58,159 --> 01:51:00,719 attributes object-oriented programming 2706 01:51:00,719 --> 01:51:03,520 comes with four key principles that you 2707 01:51:03,520 --> 01:51:05,360 should be aware of so you will 2708 01:51:05,360 --> 01:51:07,840 understand how you should design your 2709 01:51:07,840 --> 01:51:10,719 large programs so it will be easier to 2710 01:51:10,719 --> 01:51:13,040 keep developing it and i'm going to talk 2711 01:51:13,040 --> 01:51:14,960 about those principles which are 2712 01:51:14,960 --> 01:51:17,840 encapsulation abstraction inheritance 2713 01:51:17,840 --> 01:51:20,560 and polymorphism so the first principle 2714 01:51:20,560 --> 01:51:22,719 will be encapsulation and we will talk 2715 01:51:22,719 --> 01:51:25,599 about this a little bit so encapsulation 2716 01:51:25,599 --> 01:51:28,400 refers to a mechanism of restricting the 2717 01:51:28,400 --> 01:51:31,199 direct access to some of our attributes 2718 01:51:31,199 --> 01:51:34,159 in a program now notice how we did a 2719 01:51:34,159 --> 01:51:36,719 great job in the last part where we 2720 01:51:36,719 --> 01:51:38,560 implemented the encapsulation principle 2721 01:51:38,560 --> 01:51:40,880 on our project so pay attention to how 2722 01:51:40,880 --> 01:51:43,280 the name attribute could not be set to a 2723 01:51:43,280 --> 01:51:46,080 new value before it goes through some 2724 01:51:46,080 --> 01:51:48,800 conditions that we set here like the 2725 01:51:48,800 --> 01:51:51,040 length of the character being less than 2726 01:51:51,040 --> 01:51:54,000 10 characters so restricting the ability 2727 01:51:54,000 --> 01:51:56,800 to override the values for your objects 2728 01:51:56,800 --> 01:51:59,840 within your setters is exactly what the 2729 01:51:59,840 --> 01:52:02,639 encapsulation principle is about now to 2730 01:52:02,639 --> 01:52:04,239 even give you a better example of 2731 01:52:04,239 --> 01:52:06,400 encapsulation principle then we are 2732 01:52:06,400 --> 01:52:08,719 going to apply similar things to an 2733 01:52:08,719 --> 01:52:10,560 additional attribute which is going to 2734 01:52:10,560 --> 01:52:13,280 be the price attribute now if we take a 2735 01:52:13,280 --> 01:52:15,360 quick look in that program that i have 2736 01:52:15,360 --> 01:52:16,560 just executed 2737 01:52:16,560 --> 01:52:18,400 then you can already see that i have the 2738 01:52:18,400 --> 01:52:21,199 ability to directly set this object into 2739 01:52:21,199 --> 01:52:23,760 whatever number that i like to also 2740 01:52:23,760 --> 01:52:26,639 negative 900 will work here so that's 2741 01:52:26,639 --> 01:52:28,480 probably something that we look to 2742 01:52:28,480 --> 01:52:30,560 change and the way that we can change 2743 01:52:30,560 --> 01:52:32,880 that is by implementing some methods 2744 01:52:32,880 --> 01:52:36,480 that will restrict the access to this 2745 01:52:36,480 --> 01:52:38,639 price attribute so it could have been 2746 01:52:38,639 --> 01:52:41,199 nice if we could have two methods that 2747 01:52:41,199 --> 01:52:43,360 will be responsible to increment this 2748 01:52:43,360 --> 01:52:45,920 price by some percentage amount and the 2749 01:52:45,920 --> 01:52:48,480 same goes for the discount now if you 2750 01:52:48,480 --> 01:52:50,719 remember we already came up with a 2751 01:52:50,719 --> 01:52:52,639 similar method that looks like apply 2752 01:52:52,639 --> 01:52:55,280 discount when we talked about class 2753 01:52:55,280 --> 01:52:57,920 attributes because self.pay rate 2754 01:52:57,920 --> 01:53:00,639 multiplied by the actual price 2755 01:53:00,639 --> 01:53:02,320 is actually going to change this 2756 01:53:02,320 --> 01:53:05,520 attribute being decreased by 20 because 2757 01:53:05,520 --> 01:53:08,480 pay rate is set to 0.8 if you remember 2758 01:53:08,480 --> 01:53:10,560 from the previous episodes so let's go 2759 01:53:10,560 --> 01:53:13,599 ahead and continue designing this price 2760 01:53:13,599 --> 01:53:15,760 attribute to totally support the 2761 01:53:15,760 --> 01:53:18,400 encapsulation principle so first things 2762 01:53:18,400 --> 01:53:20,480 first i'm going to convert this price 2763 01:53:20,480 --> 01:53:22,639 into being a private attribute so it 2764 01:53:22,639 --> 01:53:25,040 will be a great start avoiding to set 2765 01:53:25,040 --> 01:53:27,040 this price directly like we have seen 2766 01:53:27,040 --> 01:53:29,679 previously now i'm not going to just add 2767 01:53:29,679 --> 01:53:31,520 here double underscore besides i'm going 2768 01:53:31,520 --> 01:53:33,760 to grab this whole thing 2769 01:53:33,760 --> 01:53:35,040 i'm going to 2770 01:53:35,040 --> 01:53:37,119 right click and then i'm going to say 2771 01:53:37,119 --> 01:53:39,840 refactor rename and then i'm just going 2772 01:53:39,840 --> 01:53:43,440 to rename this price by setting it like 2773 01:53:43,440 --> 01:53:45,520 that double underscore before that now 2774 01:53:45,520 --> 01:53:47,599 doing this is actually going to refactor 2775 01:53:47,599 --> 01:53:50,320 this change on the entire class where we 2776 01:53:50,320 --> 01:53:52,719 try to access the price attribute so 2777 01:53:52,719 --> 01:53:54,880 that is a great thing so we don't really 2778 01:53:54,880 --> 01:53:56,960 have to change everywhere so once i have 2779 01:53:56,960 --> 01:53:58,880 done that then if we would also take a 2780 01:53:58,880 --> 01:54:01,040 look in the apply discount then you can 2781 01:54:01,040 --> 01:54:04,320 see that we have this being set to a new 2782 01:54:04,320 --> 01:54:06,800 variable name that we came up with so 2783 01:54:06,800 --> 01:54:08,960 now that we have done this then let's go 2784 01:54:08,960 --> 01:54:12,000 ahead and create a property so we will 2785 01:54:12,000 --> 01:54:14,000 have the ability to access the price 2786 01:54:14,000 --> 01:54:15,440 attribute 2787 01:54:15,440 --> 01:54:18,080 temporarily being only read only so i'm 2788 01:54:18,080 --> 01:54:21,040 going to say add property and then i'm 2789 01:54:21,040 --> 01:54:23,199 going to say def 2790 01:54:23,199 --> 01:54:24,320 price 2791 01:54:24,320 --> 01:54:26,560 then i'm just going to return 2792 01:54:26,560 --> 01:54:28,000 self. 2793 01:54:28,000 --> 01:54:30,719 price so that's a great starter to 2794 01:54:30,719 --> 01:54:32,560 restrict the access to the price 2795 01:54:32,560 --> 01:54:34,880 attribute because now we still have 2796 01:54:34,880 --> 01:54:37,679 access to the price attribute 2797 01:54:37,679 --> 01:54:40,080 and then we cannot set that so you can 2798 01:54:40,080 --> 01:54:42,400 see that if we were to try to access 2799 01:54:42,400 --> 01:54:44,159 item1.price 2800 01:54:44,159 --> 01:54:47,599 we will have some errors but we can just 2801 01:54:47,599 --> 01:54:49,360 access the 2802 01:54:49,360 --> 01:54:52,080 actual value of that where it comes from 2803 01:54:52,080 --> 01:54:54,960 the initialization so that's a 2804 01:54:54,960 --> 01:54:57,040 oh actually i see that we hit an error 2805 01:54:57,040 --> 01:54:59,199 that says recursion error and that's 2806 01:54:59,199 --> 01:55:00,560 probably because i did not add the 2807 01:55:00,560 --> 01:55:03,199 double underscore in here by mistake so 2808 01:55:03,199 --> 01:55:04,960 that's actually a great exception that 2809 01:55:04,960 --> 01:55:07,440 we came across right now you can see 2810 01:55:07,440 --> 01:55:09,760 that we are going to hit recursion error 2811 01:55:09,760 --> 01:55:12,239 maximum recursion depth exceeded and 2812 01:55:12,239 --> 01:55:14,000 that happened because i tried to return 2813 01:55:14,000 --> 01:55:15,920 the price without the double underscore 2814 01:55:15,920 --> 01:55:16,719 so 2815 01:55:16,719 --> 01:55:19,760 if we try to call the self.price then it 2816 01:55:19,760 --> 01:55:22,000 is going to try to call this function 2817 01:55:22,000 --> 01:55:23,920 and if we try to return that then it's 2818 01:55:23,920 --> 01:55:26,080 just going to loop over it again and in 2819 01:55:26,080 --> 01:55:28,320 some time it's going to fail with the 2820 01:55:28,320 --> 01:55:30,719 recursion error as you see so that's 2821 01:55:30,719 --> 01:55:32,480 actually a great exception that 2822 01:55:32,480 --> 01:55:35,119 we see now and if you see this exception 2823 01:55:35,119 --> 01:55:36,960 in your object-oriented programs now you 2824 01:55:36,960 --> 01:55:39,440 know how to handle it and if i was to 2825 01:55:39,440 --> 01:55:42,000 come back now to main and execute that 2826 01:55:42,000 --> 01:55:43,679 then you can see that the expected 2827 01:55:43,679 --> 01:55:46,560 result is here all right so now that we 2828 01:55:46,560 --> 01:55:48,639 have done this then let's go back to our 2829 01:55:48,639 --> 01:55:51,679 class and try to work on our methods 2830 01:55:51,679 --> 01:55:54,639 that will modify the attribute of double 2831 01:55:54,639 --> 01:55:57,920 underscore price so i will actually cut 2832 01:55:57,920 --> 01:56:01,280 this method from here and i will just 2833 01:56:01,280 --> 01:56:02,400 put that 2834 01:56:02,400 --> 01:56:05,599 right under the property price that we 2835 01:56:05,599 --> 01:56:07,920 came up with so we will have a cleaner 2836 01:56:07,920 --> 01:56:10,639 look now first things first you can see 2837 01:56:10,639 --> 01:56:12,960 that we have the apply discount and we 2838 01:56:12,960 --> 01:56:16,960 will also like to come up with apply 2839 01:56:16,960 --> 01:56:19,199 um increments 2840 01:56:19,199 --> 01:56:21,840 like the following and we would like to 2841 01:56:21,840 --> 01:56:23,040 say here 2842 01:56:23,040 --> 01:56:24,719 self dot 2843 01:56:24,719 --> 01:56:28,480 double underscore price is equal to self 2844 01:56:28,480 --> 01:56:30,960 dot double underscore price 2845 01:56:30,960 --> 01:56:32,159 plus 2846 01:56:32,159 --> 01:56:34,800 self dot double underscore price 2847 01:56:34,800 --> 01:56:37,760 multiplied by some value that we can 2848 01:56:37,760 --> 01:56:40,000 receive as a perimeter so we could 2849 01:56:40,000 --> 01:56:42,400 actually receive a parameter 2850 01:56:42,400 --> 01:56:45,199 that we could name increment 2851 01:56:45,199 --> 01:56:46,239 value 2852 01:56:46,239 --> 01:56:48,800 and then we could just multiply it by 2853 01:56:48,800 --> 01:56:51,199 that number so now that we came up with 2854 01:56:51,199 --> 01:56:53,760 this then let's test this up okay i'm 2855 01:56:53,760 --> 01:56:57,280 going to go back to my main.py and then 2856 01:56:57,280 --> 01:57:01,280 i'm going to call item1 dot apply 2857 01:57:01,280 --> 01:57:03,440 increment and then i'm just going to 2858 01:57:03,440 --> 01:57:07,280 pass in 0.2 so we will increment the 2859 01:57:07,280 --> 01:57:10,000 value of 750 2860 01:57:10,000 --> 01:57:11,679 by 20 2861 01:57:11,679 --> 01:57:14,159 now the next time that i access the item 2862 01:57:14,159 --> 01:57:15,599 one dot price 2863 01:57:15,599 --> 01:57:17,920 we should be able to see the actual 2864 01:57:17,920 --> 01:57:21,679 value of price which should be 900 and 2865 01:57:21,679 --> 01:57:23,760 if i was to run that then you can see 2866 01:57:23,760 --> 01:57:26,639 that the price has been incremented to 2867 01:57:26,639 --> 01:57:30,560 900 as expected so that is exactly 2868 01:57:30,560 --> 01:57:32,960 encapsulation in action because you go 2869 01:57:32,960 --> 01:57:35,520 ahead and you don't allow the access 2870 01:57:35,520 --> 01:57:38,400 directly to the price attribute besides 2871 01:57:38,400 --> 01:57:41,520 you modify this attribute by using 2872 01:57:41,520 --> 01:57:44,000 methods like apply increment or apply 2873 01:57:44,000 --> 01:57:46,480 discount now the same will happen if i 2874 01:57:46,480 --> 01:57:50,239 was to now go ahead and use item1 dot 2875 01:57:50,239 --> 01:57:51,599 apply 2876 01:57:51,599 --> 01:57:53,440 discount and you can actually modify 2877 01:57:53,440 --> 01:57:55,840 this method in the way that you like to 2878 01:57:55,840 --> 01:57:58,000 but this currently refers to a class 2879 01:57:58,000 --> 01:58:00,239 attribute that we use 2880 01:58:00,239 --> 01:58:03,119 here so this should also again apply a 2881 01:58:03,119 --> 01:58:06,960 discount of 20 to the 900 price we 2882 01:58:06,960 --> 01:58:09,920 should be able to see 720 2883 01:58:09,920 --> 01:58:11,920 and that's exactly what is happening 2884 01:58:11,920 --> 01:58:14,080 here all right so the next principle 2885 01:58:14,080 --> 01:58:16,000 that i'm going to talk about is called 2886 01:58:16,000 --> 01:58:18,639 abstraction now abstraction is the 2887 01:58:18,639 --> 01:58:21,280 concept of object-oriented programming 2888 01:58:21,280 --> 01:58:24,800 that only shows the necessary attributes 2889 01:58:24,800 --> 01:58:27,760 and hides the unnecessary information 2890 01:58:27,760 --> 01:58:30,719 now the main purpose of abstraction is 2891 01:58:30,719 --> 01:58:33,040 basically hiding unnecessary details 2892 01:58:33,040 --> 01:58:36,239 from the users now by seeing users i 2893 01:58:36,239 --> 01:58:39,040 basically mean people like me or you 2894 01:58:39,040 --> 01:58:41,520 that are going to use the item class to 2895 01:58:41,520 --> 01:58:44,560 create some item objects now you can see 2896 01:58:44,560 --> 01:58:46,960 that now we have a new program here that 2897 01:58:46,960 --> 01:58:50,159 has one item object that its name is my 2898 01:58:50,159 --> 01:58:52,800 item price being that number and we have 2899 01:58:52,800 --> 01:58:55,760 six from this item now you can also see 2900 01:58:55,760 --> 01:58:57,840 that i came up with a method that 2901 01:58:57,840 --> 01:58:59,840 doesn't really exist which is called 2902 01:58:59,840 --> 01:59:02,560 send email so that method at the end of 2903 01:59:02,560 --> 01:59:04,880 the day should send an email to someone 2904 01:59:04,880 --> 01:59:07,440 that would like to decide about this 2905 01:59:07,440 --> 01:59:10,000 item and it will send info about how 2906 01:59:10,000 --> 01:59:12,000 much money we can earn by selling all 2907 01:59:12,000 --> 01:59:15,360 the items and maybe about some more info 2908 01:59:15,360 --> 01:59:18,480 that is related to this item now 2909 01:59:18,480 --> 01:59:21,360 sending an email is not as easy action 2910 01:59:21,360 --> 01:59:24,239 just by calling it with that way 2911 01:59:24,239 --> 01:59:26,800 because in the background email sending 2912 01:59:26,800 --> 01:59:29,440 has to go a lot of processes like 2913 01:59:29,440 --> 01:59:32,159 connecting to an smtp server and as well 2914 01:59:32,159 --> 01:59:35,360 as preparing the body of the email with 2915 01:59:35,360 --> 01:59:37,599 an auto message that will display some 2916 01:59:37,599 --> 01:59:39,840 info about this item so as we can 2917 01:59:39,840 --> 01:59:41,520 understand we have to go through a lot 2918 01:59:41,520 --> 01:59:44,080 of new methods before we go ahead and 2919 01:59:44,080 --> 01:59:47,360 just call a send email method so to 2920 01:59:47,360 --> 01:59:49,440 simulate that then i can actually go 2921 01:59:49,440 --> 01:59:52,000 ahead and say send email so i will just 2922 01:59:52,000 --> 01:59:54,159 create the method that is necessary 2923 01:59:54,159 --> 01:59:57,040 temporarily i will use pass now as i 2924 01:59:57,040 --> 01:59:59,679 said we also have to go through a lot of 2925 01:59:59,679 --> 02:00:01,840 other processes so it is a great idea to 2926 02:00:01,840 --> 02:00:04,000 create methods for each of those 2927 02:00:04,000 --> 02:00:07,440 processes like connecting to an smtp 2928 02:00:07,440 --> 02:00:08,480 server 2929 02:00:08,480 --> 02:00:11,440 smtp server like that and i will just 2930 02:00:11,440 --> 02:00:13,760 say pass because we only try to simulate 2931 02:00:13,760 --> 02:00:16,080 the idea of abstraction here i'm not 2932 02:00:16,080 --> 02:00:18,080 really going to send an actual email to 2933 02:00:18,080 --> 02:00:20,719 someone i'm just simulating an idea of 2934 02:00:20,719 --> 02:00:22,960 sending an email and we will also have 2935 02:00:22,960 --> 02:00:26,000 to go through preparing a body for an 2936 02:00:26,000 --> 02:00:28,719 automatic mail prepare 2937 02:00:28,719 --> 02:00:32,239 body right and then i can just return a 2938 02:00:32,239 --> 02:00:35,599 formatted string that will say 2939 02:00:35,599 --> 02:00:38,560 something like hello 2940 02:00:38,560 --> 02:00:40,159 um 2941 02:00:40,159 --> 02:00:41,520 someone 2942 02:00:41,520 --> 02:00:43,280 we could receive this as a parameter as 2943 02:00:43,280 --> 02:00:46,000 well and then we can say 2944 02:00:46,000 --> 02:00:48,320 we have 2945 02:00:48,320 --> 02:00:51,320 self.name 2946 02:00:51,840 --> 02:00:54,480 six times right so it should be six 2947 02:00:54,480 --> 02:00:57,520 self dot quantity times like that 2948 02:00:57,520 --> 02:01:01,119 then i can say regards 2949 02:01:01,119 --> 02:01:03,599 to shape code so that is just a very 2950 02:01:03,599 --> 02:01:06,880 simple email that we can send to someone 2951 02:01:06,880 --> 02:01:08,560 now you can understand that we have to 2952 02:01:08,560 --> 02:01:11,679 call those methods inside the send email 2953 02:01:11,679 --> 02:01:15,040 so simulating that will be self dot 2954 02:01:15,040 --> 02:01:16,000 connect 2955 02:01:16,000 --> 02:01:18,159 and then self dot 2956 02:01:18,159 --> 02:01:19,599 and 2957 02:01:19,599 --> 02:01:21,199 prepare body 2958 02:01:21,199 --> 02:01:23,599 and probably we also need to go through 2959 02:01:23,599 --> 02:01:26,560 sending it right so we can just 2960 02:01:26,560 --> 02:01:29,199 say something like send here 2961 02:01:29,199 --> 02:01:31,840 then use pass again now you can see that 2962 02:01:31,840 --> 02:01:33,199 those methods 2963 02:01:33,199 --> 02:01:35,119 at the end of the day are only going to 2964 02:01:35,119 --> 02:01:37,840 be called from the send email because 2965 02:01:37,840 --> 02:01:40,960 those are just parts of the email 2966 02:01:40,960 --> 02:01:44,239 sending process that is a bigger process 2967 02:01:44,239 --> 02:01:46,639 that we divided into multiple steps in 2968 02:01:46,639 --> 02:01:50,239 this class now the biggest problem is we 2969 02:01:50,239 --> 02:01:52,000 will have access 2970 02:01:52,000 --> 02:01:53,760 calling those methods 2971 02:01:53,760 --> 02:01:56,239 from the instance and that is exactly 2972 02:01:56,239 --> 02:01:58,880 what abstraction is about abstraction 2973 02:01:58,880 --> 02:02:01,280 principle says to you that you should 2974 02:02:01,280 --> 02:02:04,480 hide unnecessary information from the 2975 02:02:04,480 --> 02:02:08,400 instances so that is why by converting 2976 02:02:08,400 --> 02:02:11,040 those methods into being private methods 2977 02:02:11,040 --> 02:02:13,599 then we actually apply abstraction 2978 02:02:13,599 --> 02:02:15,840 principles and that is achievable in 2979 02:02:15,840 --> 02:02:18,159 python in a way which i'm going to be 2980 02:02:18,159 --> 02:02:21,119 honest is not too much convenient but it 2981 02:02:21,119 --> 02:02:23,599 is achievable by adding double 2982 02:02:23,599 --> 02:02:25,679 underscore now again in other 2983 02:02:25,679 --> 02:02:28,560 programming languages this is achievable 2984 02:02:28,560 --> 02:02:31,280 by having access modifiers for your 2985 02:02:31,280 --> 02:02:34,320 methods like private or public and i'm 2986 02:02:34,320 --> 02:02:35,599 talking about 2987 02:02:35,599 --> 02:02:38,159 programming languages like java c-sharp 2988 02:02:38,159 --> 02:02:41,199 etc so if we were to convert those 2989 02:02:41,199 --> 02:02:43,520 methods to private methods by only 2990 02:02:43,520 --> 02:02:46,719 adding double underscore then those only 2991 02:02:46,719 --> 02:02:48,159 could be called 2992 02:02:48,159 --> 02:02:51,040 from the class level meaning inside the 2993 02:02:51,040 --> 02:02:53,920 class so if we were to try to access it 2994 02:02:53,920 --> 02:02:55,679 then you can see that i'm going to have 2995 02:02:55,679 --> 02:02:58,960 auto completion meaning i will have the 2996 02:02:58,960 --> 02:03:01,599 ability to access those methods 2997 02:03:01,599 --> 02:03:04,480 and then i will just do the same for 2998 02:03:04,480 --> 02:03:07,040 the other methods now this error comes 2999 02:03:07,040 --> 02:03:08,639 from here because we did not really 3000 02:03:08,639 --> 02:03:11,280 specify some argument i'm just going to 3001 02:03:11,280 --> 02:03:13,679 add an empty string now if i was to go 3002 02:03:13,679 --> 02:03:15,840 back to our main.py file then you're 3003 02:03:15,840 --> 02:03:17,440 gonna see that we are going to have some 3004 02:03:17,440 --> 02:03:19,760 troubles even if i'm going to try to 3005 02:03:19,760 --> 02:03:21,760 access it with a double underscore i'm 3006 02:03:21,760 --> 02:03:24,400 not even going to have an autocompletion 3007 02:03:24,400 --> 02:03:26,000 and the reason for that it is because 3008 02:03:26,000 --> 02:03:28,320 that is a private method so you really 3009 02:03:28,320 --> 02:03:30,480 have to think about your methods if you 3010 02:03:30,480 --> 02:03:32,960 want to make them accessible outside of 3011 02:03:32,960 --> 02:03:35,840 your class meaning from the instances 3012 02:03:35,840 --> 02:03:38,320 and that is exactly what abstraction is 3013 02:03:38,320 --> 02:03:40,400 about you want to abstract the 3014 02:03:40,400 --> 02:03:43,599 information that is unnecessary to call 3015 02:03:43,599 --> 02:03:47,040 it or to access it from your instances 3016 02:03:47,040 --> 02:03:49,119 okay so inheritance is the third 3017 02:03:49,119 --> 02:03:51,679 principle of object-oriented programming 3018 02:03:51,679 --> 02:03:53,920 inheritance is a mechanism that allows 3019 02:03:53,920 --> 02:03:57,840 us to reuse code across our classes now 3020 02:03:57,840 --> 02:03:59,679 that's something that we have totally 3021 02:03:59,679 --> 02:04:02,079 designed well throughout this course 3022 02:04:02,079 --> 02:04:04,719 because we came up with more classes 3023 02:04:04,719 --> 02:04:07,440 that are child classes of the item class 3024 02:04:07,440 --> 02:04:10,719 where each child class represents a kind 3025 02:04:10,719 --> 02:04:12,159 of an item 3026 02:04:12,159 --> 02:04:14,320 now pay attention how i changed the 3027 02:04:14,320 --> 02:04:16,159 import line from 3028 02:04:16,159 --> 02:04:19,199 phone import phone and i use the child 3029 02:04:19,199 --> 02:04:21,280 class of item which we came up with 3030 02:04:21,280 --> 02:04:23,599 which is called phone you can see that 3031 02:04:23,599 --> 02:04:26,159 i'm passing similar arguments and i can 3032 02:04:26,159 --> 02:04:29,119 still use a code that is implemented in 3033 02:04:29,119 --> 02:04:31,440 the parent class if we were to execute 3034 02:04:31,440 --> 02:04:33,119 this program then we are not going to 3035 02:04:33,119 --> 02:04:35,920 receive any problems because 3036 02:04:35,920 --> 02:04:37,119 phone 3037 02:04:37,119 --> 02:04:40,880 uses all the methods and attributes that 3038 02:04:40,880 --> 02:04:43,360 it inherits from the item class which is 3039 02:04:43,360 --> 02:04:45,760 exactly here and if we remember we 3040 02:04:45,760 --> 02:04:48,000 designed the send email method in the 3041 02:04:48,000 --> 02:04:50,159 parent class and we can use it from the 3042 02:04:50,159 --> 02:04:52,639 instance of a phone and we can also do 3043 02:04:52,639 --> 02:04:54,719 that for the rest of the methods that we 3044 02:04:54,719 --> 02:04:57,119 came up with that really affects some of 3045 02:04:57,119 --> 02:04:59,679 the attributes like in the 3046 02:04:59,679 --> 02:05:02,719 encapsulation part where we applied 3047 02:05:02,719 --> 02:05:05,679 the apply increment method that receives 3048 02:05:05,679 --> 02:05:08,000 an increment value and if we were to 3049 02:05:08,000 --> 02:05:09,440 test this 3050 02:05:09,440 --> 02:05:13,440 incrementing the price by 0.2 and then 3051 02:05:13,440 --> 02:05:16,320 printing item one dot price then we 3052 02:05:16,320 --> 02:05:19,119 should see 1200 so if we were to print 3053 02:05:19,119 --> 02:05:21,119 that then you can see that that is 3054 02:05:21,119 --> 02:05:23,599 exactly the result so that is mainly 3055 02:05:23,599 --> 02:05:26,239 what inheritance is about it is about 3056 02:05:26,239 --> 02:05:29,280 reusing the code across all the classes 3057 02:05:29,280 --> 02:05:31,679 and that's exactly the scenario here and 3058 02:05:31,679 --> 02:05:34,000 the beauty behind that is that you can 3059 02:05:34,000 --> 02:05:36,400 come up with more tried classes that 3060 02:05:36,400 --> 02:05:39,119 will represent kinds of items like 3061 02:05:39,119 --> 02:05:41,520 laptop keyboard camera everything that 3062 02:05:41,520 --> 02:05:43,920 you think about and then you can just 3063 02:05:43,920 --> 02:05:46,560 modify specific methods that you'd like 3064 02:05:46,560 --> 02:05:48,800 to according to the kind of item that 3065 02:05:48,800 --> 02:05:51,920 you have so that's perfectly describing 3066 02:05:51,920 --> 02:05:54,880 what inheritance is about all right so 3067 02:05:54,880 --> 02:05:57,520 the last principle that we have now is 3068 02:05:57,520 --> 02:06:00,400 polymorphism now polymorphism is a very 3069 02:06:00,400 --> 02:06:02,480 important concept in programming it 3070 02:06:02,480 --> 02:06:06,159 refers to use of a single type entity to 3071 02:06:06,159 --> 02:06:08,239 represent different types in different 3072 02:06:08,239 --> 02:06:11,440 scenarios now a perfect example for this 3073 02:06:11,440 --> 02:06:14,239 definition will be some of the functions 3074 02:06:14,239 --> 02:06:16,079 that we already know that exist in 3075 02:06:16,079 --> 02:06:18,639 python now just a quick side note 3076 02:06:18,639 --> 02:06:22,639 polymorphism refers to many forms poly 3077 02:06:22,639 --> 02:06:26,400 being many and morphism being forms so 3078 02:06:26,400 --> 02:06:29,040 again the idea of applying polymorphism 3079 02:06:29,040 --> 02:06:31,679 on our programs is the ability to have 3080 02:06:31,679 --> 02:06:34,480 different scenarios when we call the 3081 02:06:34,480 --> 02:06:37,520 exact same entity and an entity could be 3082 02:06:37,520 --> 02:06:39,520 a function that we just call now as you 3083 02:06:39,520 --> 02:06:41,520 can understand polymorphism isn't 3084 02:06:41,520 --> 02:06:43,679 something that is specifically applied 3085 02:06:43,679 --> 02:06:46,079 to how you create your classes that is 3086 02:06:46,079 --> 02:06:48,400 actually something that refers globally 3087 02:06:48,400 --> 02:06:50,719 to your entire project and in the next 3088 02:06:50,719 --> 02:06:52,639 few minutes we are going to see some bad 3089 02:06:52,639 --> 02:06:54,880 practices where polymorphism is not 3090 02:06:54,880 --> 02:06:56,800 applied and we are also going to see 3091 02:06:56,800 --> 02:06:58,800 where in python polymorphism is 3092 02:06:58,800 --> 02:07:01,760 perfectly applied so a great example of 3093 02:07:01,760 --> 02:07:04,400 where polymorphism is applied is in the 3094 02:07:04,400 --> 02:07:07,280 lan built-in function because the lan 3095 02:07:07,280 --> 02:07:10,239 built-in function in python knows how to 3096 02:07:10,239 --> 02:07:12,880 handle different kinds of objects that 3097 02:07:12,880 --> 02:07:15,440 it receives as an argument and it 3098 02:07:15,440 --> 02:07:18,159 returns you a result accordingly so as 3099 02:07:18,159 --> 02:07:20,960 you can see in here if we were to use 3100 02:07:20,960 --> 02:07:24,560 the land built-in function in a string 3101 02:07:24,560 --> 02:07:26,960 then we will receive the total amount of 3102 02:07:26,960 --> 02:07:30,960 characters but if we will do this in a 3103 02:07:30,960 --> 02:07:33,360 list then we will not receive the length 3104 02:07:33,360 --> 02:07:35,360 of characters of this entire expression 3105 02:07:35,360 --> 02:07:38,400 in here besides we will receive back the 3106 02:07:38,400 --> 02:07:41,440 amount of elements that are inside this 3107 02:07:41,440 --> 02:07:43,679 list and to show you how this is going 3108 02:07:43,679 --> 02:07:46,000 to work then i'm just going to run this 3109 02:07:46,000 --> 02:07:48,159 program and for sure the results are 3110 02:07:48,159 --> 02:07:50,960 just as expected so as the definition of 3111 02:07:50,960 --> 02:07:53,599 polymorphism says it is just a single 3112 02:07:53,599 --> 02:07:55,920 entity that does know how to handle 3113 02:07:55,920 --> 02:07:59,199 different kinds of objects as expected 3114 02:07:59,199 --> 02:08:01,199 all right so now that we understood that 3115 02:08:01,199 --> 02:08:03,440 the polymorphism is applied everywhere 3116 02:08:03,440 --> 02:08:05,199 in python especially in the land 3117 02:08:05,199 --> 02:08:07,440 building function let's also understand 3118 02:08:07,440 --> 02:08:09,840 where it is implemented on our project 3119 02:08:09,840 --> 02:08:12,079 here now you can see that i try to call 3120 02:08:12,079 --> 02:08:14,480 the apply discount method that is never 3121 02:08:14,480 --> 02:08:16,719 implemented inside the phone class and 3122 02:08:16,719 --> 02:08:19,840 the fact that i can use it from the item 3123 02:08:19,840 --> 02:08:21,920 class it is because we inherit from this 3124 02:08:21,920 --> 02:08:25,440 class and that is the basically reason 3125 02:08:25,440 --> 02:08:27,599 now if i was to go back to that main dot 3126 02:08:27,599 --> 02:08:30,000 py file and run this then you can see 3127 02:08:30,000 --> 02:08:32,000 that that is going to work because the 3128 02:08:32,000 --> 02:08:34,880 applied discount is a method that i can 3129 02:08:34,880 --> 02:08:38,000 use from the inherited item class now 3130 02:08:38,000 --> 02:08:39,760 that's exactly where polymorphism is 3131 02:08:39,760 --> 02:08:42,560 also in action because polymorphism 3132 02:08:42,560 --> 02:08:44,960 again refers to one single entity that 3133 02:08:44,960 --> 02:08:48,400 you can use from multiple objects now if 3134 02:08:48,400 --> 02:08:50,880 i was one day to go ahead and create 3135 02:08:50,880 --> 02:08:54,719 more kinds of items meaning more classes 3136 02:08:54,719 --> 02:08:56,480 that will represent different kinds of 3137 02:08:56,480 --> 02:08:58,880 items and from them to call the applied 3138 02:08:58,880 --> 02:09:01,199 discount method that's also going to 3139 02:09:01,199 --> 02:09:04,079 work because the applied discount is a 3140 02:09:04,079 --> 02:09:05,840 method that is going to be accessible 3141 02:09:05,840 --> 02:09:08,079 from all the kinds of objects so that's 3142 02:09:08,079 --> 02:09:10,000 exactly why you might have heard about 3143 02:09:10,000 --> 02:09:11,440 the terms of inheritance and 3144 02:09:11,440 --> 02:09:14,320 polymorphism together combined now to 3145 02:09:14,320 --> 02:09:16,800 show you that then let's try to create 3146 02:09:16,800 --> 02:09:18,159 one more class that is going to be 3147 02:09:18,159 --> 02:09:20,239 identical to the phone class right i'm 3148 02:09:20,239 --> 02:09:24,000 going to create a keyboard file and then 3149 02:09:24,000 --> 02:09:26,000 i'm just going to say here 3150 02:09:26,000 --> 02:09:27,280 class 3151 02:09:27,280 --> 02:09:29,119 you know what before that let's go to 3152 02:09:29,119 --> 02:09:29,920 phone 3153 02:09:29,920 --> 02:09:31,040 and 3154 02:09:31,040 --> 02:09:33,280 copy everything from here and paste this 3155 02:09:33,280 --> 02:09:36,480 in like that i'm going to get rid from 3156 02:09:36,480 --> 02:09:38,320 those lines 3157 02:09:38,320 --> 02:09:40,480 and i'm just going to leave the init as 3158 02:09:40,480 --> 02:09:43,599 it is i'm going to change the class name 3159 02:09:43,599 --> 02:09:44,560 from 3160 02:09:44,560 --> 02:09:46,880 phone to keyboard and i'm also going to 3161 02:09:46,880 --> 02:09:49,199 delete that attribute that we don't need 3162 02:09:49,199 --> 02:09:51,679 broken phones all right so now that we 3163 02:09:51,679 --> 02:09:54,320 have this then i can actually go ahead 3164 02:09:54,320 --> 02:09:57,280 to my main.py file and use one more 3165 02:09:57,280 --> 02:09:59,679 import line that will say from 3166 02:09:59,679 --> 02:10:02,480 keyboard import keyboard and then i'm 3167 02:10:02,480 --> 02:10:05,520 going to change this to keyboard 3168 02:10:05,520 --> 02:10:08,000 will replace this name just to make it 3169 02:10:08,000 --> 02:10:10,159 more realistic then i'm going to run the 3170 02:10:10,159 --> 02:10:11,760 same program 3171 02:10:11,760 --> 02:10:14,079 you can see that this works so that's 3172 02:10:14,079 --> 02:10:16,800 again exactly where polymorphism is in 3173 02:10:16,800 --> 02:10:18,639 action because we can use this single 3174 02:10:18,639 --> 02:10:21,119 entity from different kinds of objects 3175 02:10:21,119 --> 02:10:22,960 and it will know how to handle it 3176 02:10:22,960 --> 02:10:25,280 properly now by saying handle it 3177 02:10:25,280 --> 02:10:28,159 properly then i basically mean you can 3178 02:10:28,159 --> 02:10:31,119 have the control of how many amount of 3179 02:10:31,119 --> 02:10:33,760 discount you want to apply inside your 3180 02:10:33,760 --> 02:10:37,199 classes now because if we were to go to 3181 02:10:37,199 --> 02:10:40,960 keyboard and use a class 3182 02:10:40,960 --> 02:10:44,000 attribute exactly like we used in the 3183 02:10:44,000 --> 02:10:46,320 item class which was 3184 02:10:46,320 --> 02:10:48,560 pay rate then we're going to have full 3185 02:10:48,560 --> 02:10:50,800 control for all the discounts that are 3186 02:10:50,800 --> 02:10:53,119 going to apply to the keyboard and to 3187 02:10:53,119 --> 02:10:55,119 show you that i'm going to attempt 3188 02:10:55,119 --> 02:10:57,360 typing in pay rate and you can see that 3189 02:10:57,360 --> 02:10:59,679 i even have auto completion because 3190 02:10:59,679 --> 02:11:02,560 overriding in the child class that is 3191 02:11:02,560 --> 02:11:05,040 legal all right so i can just say pay 3192 02:11:05,040 --> 02:11:08,719 rate is equal to 0.7 and that will be it 3193 02:11:08,719 --> 02:11:12,079 now i have the same discount amount for 3194 02:11:12,079 --> 02:11:14,880 all my keyboards if i was again to run 3195 02:11:14,880 --> 02:11:17,280 the main.py file then you can see that 3196 02:11:17,280 --> 02:11:20,400 the results are just as expected we see 3197 02:11:20,400 --> 02:11:23,760 700 so that is the beauty behind 3198 02:11:23,760 --> 02:11:26,400 inheritance and polymorphism together 3199 02:11:26,400 --> 02:11:28,960 and the same will go for sure if we were 3200 02:11:28,960 --> 02:11:31,440 to decide that we'd like to have 50 3201 02:11:31,440 --> 02:11:33,840 discount so it will only take from me to 3202 02:11:33,840 --> 02:11:36,239 go ahead and say pay rate is equal to 3203 02:11:36,239 --> 02:11:39,920 0.5 and that's it so i hope that you 3204 02:11:39,920 --> 02:11:41,920 understand about polymorphism a bit 3205 02:11:41,920 --> 02:11:44,639 better now now just a quick side note 3206 02:11:44,639 --> 02:11:46,800 polymorphism is perfectly could be 3207 02:11:46,800 --> 02:11:49,599 implemented by using abstract classes 3208 02:11:49,599 --> 02:11:52,000 and that is just the identical way of 3209 02:11:52,000 --> 02:11:54,000 using interfaces in other programming 3210 02:11:54,000 --> 02:11:57,199 languages like java interface is a way 3211 02:11:57,199 --> 02:11:59,679 that you can implement how a class 3212 02:11:59,679 --> 02:12:00,719 should be 3213 02:12:00,719 --> 02:12:03,520 designed right so it is like a template 3214 02:12:03,520 --> 02:12:05,840 for a class and that is achievable by 3215 02:12:05,840 --> 02:12:07,920 using abstract classes which i'm not 3216 02:12:07,920 --> 02:12:10,800 going to cover in that part but again 3217 02:12:10,800 --> 02:12:14,079 polymorphism like i said is a term that 3218 02:12:14,079 --> 02:12:16,960 is implemented in different areas in the 3219 02:12:16,960 --> 02:12:19,760 whole python programming language so i 3220 02:12:19,760 --> 02:12:21,679 hope you had a great time learning the 3221 02:12:21,679 --> 02:12:23,760 object-oriented programming course now 3222 02:12:23,760 --> 02:12:25,920 you have a lot of tools that you can go 3223 02:12:25,920 --> 02:12:28,079 ahead and try to implement by yourself 3224 02:12:28,079 --> 02:12:30,079 on your projects which will really help 3225 02:12:30,079 --> 02:12:32,480 you to take you to the next step as a 3226 02:12:32,480 --> 02:12:36,480 developer see you next time 231155

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