1 99:59:59,999 --> 99:59:59,999 So, it's 2 99:59:59,999 --> 99:59:59,999 Monty Python Flying Circus 3 99:59:59,999 --> 99:59:59,999 okay actually it is not but just so you know 4 99:59:59,999 --> 99:59:59,999 this is where the python language got its name 5 99:59:59,999 --> 99:59:59,999 it is from the comedy but today's talk will be about OOP or object-orientated programming in python 6 99:59:59,999 --> 99:59:59,999 And I am Junxi, I am a second-year student from Singapore University of Technology and Design 7 99:59:59,999 --> 99:59:59,999 currently in the Computer Science / info systems major 8 99:59:59,999 --> 99:59:59,999 And I am also currently an intern in Tinkerbox 9 99:59:59,999 --> 99:59:59,999 In fact, it is my fellow intern and TechLadies' Kate who invited to speak here today 10 99:59:59,999 --> 99:59:59,999 so I am glad to here 11 99:59:59,999 --> 99:59:59,999 so now i understand that most of the audience here are beginner level 12 99:59:59,999 --> 99:59:59,999 Just to get a gauge, how many of you guys have done OOP in python before? 13 99:59:59,999 --> 99:59:59,999 Okay, I will say that for those who have not done anything 14 99:59:59,999 --> 99:59:59,999 I hope that by the end of this talk 15 99:59:59,999 --> 99:59:59,999 we will get to understand more about 16 99:59:59,999 --> 99:59:59,999 what OOP is about 17 99:59:59,999 --> 99:59:59,999 'Kay, I would say OOP is like an intermediate level topic 18 99:59:59,999 --> 99:59:59,999 but it is a very key topic for you to understand 19 99:59:59,999 --> 99:59:59,999 for you to move on to do more advanced stuff 20 99:59:59,999 --> 99:59:59,999 so this is why I chose the topic for today 21 99:59:59,999 --> 99:59:59,999 but also it is a very broad topic 22 99:59:59,999 --> 99:59:59,999 so i don't know how much I can cover in 20 minutes 23 99:59:59,999 --> 99:59:59,999 So forgive me if I speak too fast 24 99:59:59,999 --> 99:59:59,999 Or skip over certain things 25 99:59:59,999 --> 99:59:59,999 okay 26 99:59:59,999 --> 99:59:59,999 so 27 99:59:59,999 --> 99:59:59,999 just a brief idea of what we will be talking about 28 99:59:59,999 --> 99:59:59,999 We will only be covering, like, nuts and bolts 29 99:59:59,999 --> 99:59:59,999 of OOP in python 30 99:59:59,999 --> 99:59:59,999 So language-specified construct syntax 31 99:59:59,999 --> 99:59:59,999 That sort of thing 32 99:59:59,999 --> 99:59:59,999 Because I believe for beginners 33 99:59:59,999 --> 99:59:59,999 this will be what you will be more interested in 34 99:59:59,999 --> 99:59:59,999 but at the same time, i hope i will be able to talk a little bit about 35 99:59:59,999 --> 99:59:59,999 what you called Object-oriented design 36 99:59:59,999 --> 99:59:59,999 principles of writing good codes 37 99:59:59,999 --> 99:59:59,999 with design principles, basically 38 99:59:59,999 --> 99:59:59,999 so 39 99:59:59,999 --> 99:59:59,999 Before I actually go into the actual materials 40 99:59:59,999 --> 99:59:59,999 I have a confession to make 41 99:59:59,999 --> 99:59:59,999 The sad truth is, okay maybe not that sad 42 99:59:59,999 --> 99:59:59,999 but the truth is that I probably enjoy drawing silly things 43 99:59:59,999 --> 99:59:59,999 more than I like programming so 44 99:59:59,999 --> 99:59:59,999 as a result, you will see a lot of 45 99:59:59,999 --> 99:59:59,999 silly drawings 46 99:59:59,999 --> 99:59:59,999 Probably more silly drawings than code 47 99:59:59,999 --> 99:59:59,999 in this talk 48 99:59:59,999 --> 99:59:59,999 but it is my hope that you know 49 99:59:59,999 --> 99:59:59,999 it will sort of give you a visual aid 50 99:59:59,999 --> 99:59:59,999 to understanding the concepts 51 99:59:59,999 --> 99:59:59,999 that I will be talking about 52 99:59:59,999 --> 99:59:59,999 Okay so 53 99:59:59,999 --> 99:59:59,999 let's dive right in 54 99:59:59,999 --> 99:59:59,999 Nuts and bolts 55 99:59:59,999 --> 99:59:59,999 so these are the topics I will be covering 56 99:59:59,999 --> 99:59:59,999 Objects Classes & Instances 57 99:59:59,999 --> 99:59:59,999 Attributes and Methods 58 99:59:59,999 --> 99:59:59,999 Inheritance and Scope 59 99:59:59,999 --> 99:59:59,999 And those words have completely 60 99:59:59,999 --> 99:59:59,999 flown over your head 61 99:59:59,999 --> 99:59:59,999 Do not fear 62 99:59:59,999 --> 99:59:59,999 by the end of this talk 63 99:59:59,999 --> 99:59:59,999 you should, hopefully, 64 99:59:59,999 --> 99:59:59,999 have a rough idea of what it means 65 99:59:59,999 --> 99:59:59,999 so 66 99:59:59,999 --> 99:59:59,999 let's jump straight into the first... 67 99:59:59,999 --> 99:59:59,999 oh wait, sorry. before that 68 99:59:59,999 --> 99:59:59,999 i just want to make it clear that 69 99:59:59,999 --> 99:59:59,999 in common use right now 70 99:59:59,999 --> 99:59:59,999 there are two main python versions 71 99:59:59,999 --> 99:59:59,999 so python 2 and python 3 72 99:59:59,999 --> 99:59:59,999 there are some differences in these 2 versions 73 99:59:59,999 --> 99:59:59,999 so as beginners 74 99:59:59,999 --> 99:59:59,999 it can be easy to trip up 75 99:59:59,999 --> 99:59:59,999 over these differences 76 99:59:59,999 --> 99:59:59,999 so just to be clear that 77 99:59:59,999 --> 99:59:59,999 when you read up resources online 78 99:59:59,999 --> 99:59:59,999 for example, you should know 79 99:59:59,999 --> 99:59:59,999 what the version 80 99:59:59,999 --> 99:59:59,999 those resources are talking about 81 99:59:59,999 --> 99:59:59,999 which python version they are talking about 82 99:59:59,999 --> 99:59:59,999 so for clarification 83 99:59:59,999 --> 99:59:59,999 just be clear 84 99:59:59,999 --> 99:59:59,999 I am using python 3 for all my examples here 85 99:59:59,999 --> 99:59:59,999 alright 86 99:59:59,999 --> 99:59:59,999 so objects 87 99:59:59,999 --> 99:59:59,999 think of objects as a way of 88 99:59:59,999 --> 99:59:59,999 logically encapsulating your code 89 99:59:59,999 --> 99:59:59,999 so objects has what we called attributes 90 99:59:59,999 --> 99:59:59,999 and methods 91 99:59:59,999 --> 99:59:59,999 so let's look at this doggeh over here 92 99:59:59,999 --> 99:59:59,999 i like to think of attributes as 93 99:59:59,999 --> 99:59:59,999 kinda like 94 99:59:59,999 --> 99:59:59,999 adjectives? 95 99:59:59,999 --> 99:59:59,999 or properties 96 99:59:59,999 --> 99:59:59,999 So for example you say 97 99:59:59,999 --> 99:59:59,999 the dog is white 98 99:59:59,999 --> 99:59:59,999 or can have a weight of 7 kg 99 99:59:59,999 --> 99:59:59,999 so these are like the attributes of the dog 100 99:59:59,999 --> 99:59:59,999 on the other hand 101 99:59:59,999 --> 99:59:59,999 methods 102 99:59:59,999 --> 99:59:59,999 they are kinda more like verbs 103 99:59:59,999 --> 99:59:59,999 a dog can wag tail and bark 104 99:59:59,999 --> 99:59:59,999 so these are the things 105 99:59:59,999 --> 99:59:59,999 the dog can do 106 99:59:59,999 --> 99:59:59,999 these are like 107 99:59:59,999 --> 99:59:59,999 methods of my dog object 108 99:59:59,999 --> 99:59:59,999 this is a slightly lame description 109 99:59:59,999 --> 99:59:59,999 but i hope can bring across 110 99:59:59,999 --> 99:59:59,999 into the field of what 111 99:59:59,999 --> 99:59:59,999 I mean by attributes and method 112 99:59:59,999 --> 99:59:59,999 and in fact, you know 113 99:59:59,999 --> 99:59:59,999 in python 114 99:59:59,999 --> 99:59:59,999 you have your variables and your functions 115 99:59:59,999 --> 99:59:59,999 right? 116 99:59:59,999 --> 99:59:59,999 so your attributes and methods 117 99:59:59,999 --> 99:59:59,999 are just your good ol' friends 118 99:59:59,999 --> 99:59:59,999 your variables and functions 119 99:59:59,999 --> 99:59:59,999 the only thing is that 120 99:59:59,999 --> 99:59:59,999 attributes are kind of like variables 121 99:59:59,999 --> 99:59:59,999 that are associated with a certain object 122 99:59:59,999 --> 99:59:59,999 and methods are functions 123 99:59:59,999 --> 99:59:59,999 that are associated with a certain object 124 99:59:59,999 --> 99:59:59,999 in the end, 125 99:59:59,999 --> 99:59:59,999 objects basically is a way of calculating data 126 99:59:59,999 --> 99:59:59,999 and a form of attributes 127 99:59:59,999 --> 99:59:59,999 with functions of procedures 128 99:59:59,999 --> 99:59:59,999 in the form of methods 129 99:59:59,999 --> 99:59:59,999 Okay 130 99:59:59,999 --> 99:59:59,999 so let's look at this dog transcribe into 131 99:59:59,999 --> 99:59:59,999 real python code 132 99:59:59,999 --> 99:59:59,999 for those who have not done any OOP in python before 133 99:59:59,999 --> 99:59:59,999 this syntax might look intimidating but 134 99:59:59,999 --> 99:59:59,999 hopefully by the end of it 135 99:59:59,999 --> 99:59:59,999 you will be able to understand 136 99:59:59,999 --> 99:59:59,999 what's going on 137 99:59:59,999 --> 99:59:59,999 The kind of object-oriented programming 138 99:59:59,999 --> 99:59:59,999 in python 139 99:59:59,999 --> 99:59:59,999 that we are dealing with 140 99:59:59,999 --> 99:59:59,999 is called class-based object oriented 141 99:59:59,999 --> 99:59:59,999 programming 142 99:59:59,999 --> 99:59:59,999 This is the kind of OOP 143 99:59:59,999 --> 99:59:59,999 you will find 144 99:59:59,999 --> 99:59:59,999 in languages like Ruby, Java, C# and etc. 145 99:59:59,999 --> 99:59:59,999 Only the most popular kind of OOP 146 99:59:59,999 --> 99:59:59,999 Just so you know, 147 99:59:59,999 --> 99:59:59,999 there are other kinds out there 148 99:59:59,999 --> 99:59:59,999 Class-based OOP 149 99:59:59,999 --> 99:59:59,999 you see here that 150 99:59:59,999 --> 99:59:59,999 I am defining this class "dog" 151 99:59:59,999 --> 99:59:59,999 you should know by now 152 99:59:59,999 --> 99:59:59,999 that in python, 153 99:59:59,999 --> 99:59:59,999 indentation is intactly(?) significant 154 99:59:59,999 --> 99:59:59,999 so all these indented code 155 99:59:59,999 --> 99:59:59,999 this is inside the class body. 156 99:59:59,999 --> 99:59:59,999 This is my class definition. 157 99:59:59,999 --> 99:59:59,999 Now I am talking about classes 158 99:59:59,999 --> 99:59:59,999 Now I have to talk about 159 99:59:59,999 --> 99:59:59,999 like explain about 160 99:59:59,999 --> 99:59:59,999 classes and instances 161 99:59:59,999 --> 99:59:59,999 Because we are talking about class-based OOP 162 99:59:59,999 --> 99:59:59,999 so what are classes and instances? 163 99:59:59,999 --> 99:59:59,999 So, think of a class as a kind of category 164 99:59:59,999 --> 99:59:59,999 where an instance is 165 99:59:59,999 --> 99:59:59,999 particular living example 166 99:59:59,999 --> 99:59:59,999 of a kind of thing 167 99:59:59,999 --> 99:59:59,999 To drive the point across, 168 99:59:59,999 --> 99:59:59,999 let me talk about babies 169 99:59:59,999 --> 99:59:59,999 Yes! Babies 170 99:59:59,999 --> 99:59:59,999 When you think of 171 99:59:59,999 --> 99:59:59,999 a baby right.. 172 99:59:59,999 --> 99:59:59,999 a human baby 173 99:59:59,999 --> 99:59:59,999 you kind of have this conception 174 99:59:59,999 --> 99:59:59,999 tectonic idea of what babies 175 99:59:59,999 --> 99:59:59,999 should look like 176 99:59:59,999 --> 99:59:59,999 sweet, tender, angel 177 99:59:59,999 --> 99:59:59,999 angelic things 178 99:59:59,999 --> 99:59:59,999 That is sort of like class 179 99:59:59,999 --> 99:59:59,999 this general category 180 99:59:59,999 --> 99:59:59,999 this general idea 181 99:59:59,999 --> 99:59:59,999 whereas if you actually 182 99:59:59,999 --> 99:59:59,999 see this real living kicking screaming 183 99:59:59,999 --> 99:59:59,999 pooping baby 184 99:59:59,999 --> 99:59:59,999 you are like 185 99:59:59,999 --> 99:59:59,999 "What's this ugly yoda-like thing?" 186 99:59:59,999 --> 99:59:59,999 That is kind of like instance 187 99:59:59,999 --> 99:59:59,999 that's the real concrete 188 99:59:59,999 --> 99:59:59,999 instance of a baby 189 99:59:59,999 --> 99:59:59,999 So I hope that kinda brings across the idea 190 99:59:59,999 --> 99:59:59,999 of what's the differences between 191 99:59:59,999 --> 99:59:59,999 the class and instance 192 99:59:59,999 --> 99:59:59,999 Let's go back to the code 193 99:59:59,999 --> 99:59:59,999 and in my class 194 99:59:59,999 --> 99:59:59,999 so in this class definition right 195 99:59:59,999 --> 99:59:59,999 when I define my class 196 99:59:59,999 --> 99:59:59,999 you can think of it as 197 99:59:59,999 --> 99:59:59,999 defining a custom data type 198 99:59:59,999 --> 99:59:59,999 so you know in python 199 99:59:59,999 --> 99:59:59,999 we have our built-in types 200 99:59:59,999 --> 99:59:59,999 we have integers 201 99:59:59,999 --> 99:59:59,999 we have strings 202 99:59:59,999 --> 99:59:59,999 we have lists 203 99:59:59,999 --> 99:59:59,999 we have dictionaries 204 99:59:59,999 --> 99:59:59,999 When you define your class 205 99:59:59,999 --> 99:59:59,999 it is as if you are building 206 99:59:59,999 --> 99:59:59,999 your own data type 207 99:59:59,999 --> 99:59:59,999 And this one data type 208 99:59:59,999 --> 99:59:59,999 has its own attributes and 209 99:59:59,999 --> 99:59:59,999 its own methods 210 99:59:59,999 --> 99:59:59,999 I will go through the stuff here later 211 99:59:59,999 --> 99:59:59,999 but let me go down to the code here 212 99:59:59,999 --> 99:59:59,999 so what you see down here 213 99:59:59,999 --> 99:59:59,999 in this line, line 13 214 99:59:59,999 --> 99:59:59,999 this is where I instantiate 215 99:59:59,999 --> 99:59:59,999 I create an instance for my class 216 99:59:59,999 --> 99:59:59,999 So I am assigning 217 99:59:59,999 --> 99:59:59,999 I am creating 218 99:59:59,999 --> 99:59:59,999 an instance called "Doggeh" 219 99:59:59,999 --> 99:59:59,999 And this the way I construct 220 99:59:59,999 --> 99:59:59,999 my instance 221 99:59:59,999 --> 99:59:59,999 by calling the, what we called 222 99:59:59,999 --> 99:59:59,999 the class constructor 223 99:59:59,999 --> 99:59:59,999 So this is the class name 224 99:59:59,999 --> 99:59:59,999 the parenthesis 225 99:59:59,999 --> 99:59:59,999 I pass in certain arguements 226 99:59:59,999 --> 99:59:59,999 to initialise my instance 227 99:59:59,999 --> 99:59:59,999 so now I have this Doggeh 228 99:59:59,999 --> 99:59:59,999 which is an instance of a dog class 229 99:59:59,999 --> 99:59:59,999 Okay 230 99:59:59,999 --> 99:59:59,999 so now that we sort of know 231 99:59:59,999 --> 99:59:59,999 what classes and instances are 232 99:59:59,999 --> 99:59:59,999 let me go back 233 99:59:59,999 --> 99:59:59,999 I will start going through the code here 234 99:59:59,999 --> 99:59:59,999 So to understand the code in here 235 99:59:59,999 --> 99:59:59,999 we have to talk about 236 99:59:59,999 --> 99:59:59,999 attributes and methods 237 99:59:59,999 --> 99:59:59,999 When we talk about attributes and methods 238 99:59:59,999 --> 99:59:59,999 There are two kinds 239 99:59:59,999 --> 99:59:59,999 They can be instance attributes or instance methods 240 99:59:59,999 --> 99:59:59,999 or they can be class attributes and 241 99:59:59,999 --> 99:59:59,999 class methods 242 99:59:59,999 --> 99:59:59,999 But for now 243 99:59:59,999 --> 99:59:59,999 I will talking about instance attributes 244 99:59:59,999 --> 99:59:59,999 and instance methods 245 99:59:59,999 --> 99:59:59,999 Because that is what we usually deal with 246 99:59:59,999 --> 99:59:59,999 Inside this class body 247 99:59:59,999 --> 99:59:59,999 I define several methods 248 99:59:59,999 --> 99:59:59,999 for my dog class 249 99:59:59,999 --> 99:59:59,999 so these are how you define functions 250 99:59:59,999 --> 99:59:59,999 I mean, methods are just functions 251 99:59:59,999 --> 99:59:59,999 but they are inside a class 252 99:59:59,999 --> 99:59:59,999 This is how I define my methods 253 99:59:59,999 --> 99:59:59,999 And 254 99:59:59,999 --> 99:59:59,999 you notice that 255 99:59:59,999 --> 99:59:59,999 this first method looks kind of special 256 99:59:59,999 --> 99:59:59,999 with the double underscore in front 257 99:59:59,999 --> 99:59:59,999 So double underscore init double underscore 258 99:59:59,999 --> 99:59:59,999 so i will be talking more about this later 259 99:59:59,999 --> 99:59:59,999 but let's look at these two 260 99:59:59,999 --> 99:59:59,999 func.. methods first 261 99:59:59,999 --> 99:59:59,999 Now you will notice something interesting 262 99:59:59,999 --> 99:59:59,999 about this method 263 99:59:59,999 --> 99:59:59,999 I mean, in the end, 264 99:59:59,999 --> 99:59:59,999 these methods are just stuff that 265 99:59:59,999 --> 99:59:59,999 they print something to the console 266 99:59:59,999 --> 99:59:59,999 right 267 99:59:59,999 --> 99:59:59,999 It is going to print "Wag Wag" or 268 99:59:59,999 --> 99:59:59,999 "Woof Woof" 269 99:59:59,999 --> 99:59:59,999 but there is something interesting about this 270 99:59:59,999 --> 99:59:59,999 in the method signature 271 99:59:59,999 --> 99:59:59,999 I am actually taking this parameter 'self' 272 99:59:59,999 --> 99:59:59,999 and 273 99:59:59,999 --> 99:59:59,999 this 'self' is actually an indication 274 99:59:59,999 --> 99:59:59,999 to you that 275 99:59:59,999 --> 99:59:59,999 these methods are instance methods 276 99:59:59,999 --> 99:59:59,999 We can invoke these instance methods 277 99:59:59,999 --> 99:59:59,999 by using this Dog syntax over here 278 99:59:59,999 --> 99:59:59,999 so I invoke "wag tail" by 279 99:59:59,999 --> 99:59:59,999 calling it Doggeh.wag_tail() 280 99:59:59,999 --> 99:59:59,999 And just to emphasize that you have 281 99:59:59,999 --> 99:59:59,999 to put the parenthesis there 282 99:59:59,999 --> 99:59:59,999 to invoke the method 283 99:59:59,999 --> 99:59:59,999