WEBVTT 99:59:59.999 --> 99:59:59.999 So, it's 99:59:59.999 --> 99:59:59.999 Monty Python Flying Circus 99:59:59.999 --> 99:59:59.999 okay actually it is not but just so you know 99:59:59.999 --> 99:59:59.999 this is where the python language got its name 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 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 99:59:59.999 --> 99:59:59.999 currently in the Computer Science / info systems major 99:59:59.999 --> 99:59:59.999 And I am also currently an intern in Tinkerbox 99:59:59.999 --> 99:59:59.999 In fact, it is my fellow intern and TechLadies' Kate who invited to speak here today 99:59:59.999 --> 99:59:59.999 so I am glad to here 99:59:59.999 --> 99:59:59.999 so now i understand that most of the audience here are beginner level 99:59:59.999 --> 99:59:59.999 Just to get a gauge, how many of you guys have done OOP in python before? 99:59:59.999 --> 99:59:59.999 Okay, I will say that for those who have not done anything 99:59:59.999 --> 99:59:59.999 I hope that by the end of this talk 99:59:59.999 --> 99:59:59.999 we will get to understand more about 99:59:59.999 --> 99:59:59.999 what OOP is about 99:59:59.999 --> 99:59:59.999 'Kay, I would say OOP is like an intermediate level topic 99:59:59.999 --> 99:59:59.999 but it is a very key topic for you to understand 99:59:59.999 --> 99:59:59.999 for you to move on to do more advanced stuff 99:59:59.999 --> 99:59:59.999 so this is why I chose the topic for today 99:59:59.999 --> 99:59:59.999 but also it is a very broad topic 99:59:59.999 --> 99:59:59.999 so i don't know how much I can cover in 20 minutes 99:59:59.999 --> 99:59:59.999 So forgive me if I speak too fast 99:59:59.999 --> 99:59:59.999 Or skip over certain things 99:59:59.999 --> 99:59:59.999 okay 99:59:59.999 --> 99:59:59.999 so 99:59:59.999 --> 99:59:59.999 just a brief idea of what we will be talking about 99:59:59.999 --> 99:59:59.999 We will only be covering, like, nuts and bolts 99:59:59.999 --> 99:59:59.999 of OOP in python 99:59:59.999 --> 99:59:59.999 So language-specified construct syntax 99:59:59.999 --> 99:59:59.999 That sort of thing 99:59:59.999 --> 99:59:59.999 Because I believe for beginners 99:59:59.999 --> 99:59:59.999 this will be what you will be more interested in 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 99:59:59.999 --> 99:59:59.999 what you called Object-oriented design 99:59:59.999 --> 99:59:59.999 principles of writing good codes 99:59:59.999 --> 99:59:59.999 with design principles, basically 99:59:59.999 --> 99:59:59.999 so 99:59:59.999 --> 99:59:59.999 Before I actually go into the actual materials 99:59:59.999 --> 99:59:59.999 I have a confession to make 99:59:59.999 --> 99:59:59.999 The sad truth is, okay maybe not that sad 99:59:59.999 --> 99:59:59.999 but the truth is that I probably enjoy drawing silly things 99:59:59.999 --> 99:59:59.999 more than I like programming so 99:59:59.999 --> 99:59:59.999 as a result, you will see a lot of 99:59:59.999 --> 99:59:59.999 silly drawings 99:59:59.999 --> 99:59:59.999 Probably more silly drawings than code 99:59:59.999 --> 99:59:59.999 in this talk 99:59:59.999 --> 99:59:59.999 but it is my hope that you know 99:59:59.999 --> 99:59:59.999 it will sort of give you a visual aid 99:59:59.999 --> 99:59:59.999 to understanding the concepts 99:59:59.999 --> 99:59:59.999 that I will be talking about 99:59:59.999 --> 99:59:59.999 Okay so 99:59:59.999 --> 99:59:59.999 let's dive right in 99:59:59.999 --> 99:59:59.999 Nuts and bolts 99:59:59.999 --> 99:59:59.999 so these are the topics I will be covering 99:59:59.999 --> 99:59:59.999 Objects Classes & Instances 99:59:59.999 --> 99:59:59.999 Attributes and Methods 99:59:59.999 --> 99:59:59.999 Inheritance and Scope 99:59:59.999 --> 99:59:59.999 And those words have completely 99:59:59.999 --> 99:59:59.999 flown over your head 99:59:59.999 --> 99:59:59.999 Do not fear 99:59:59.999 --> 99:59:59.999 by the end of this talk 99:59:59.999 --> 99:59:59.999 you should, hopefully, 99:59:59.999 --> 99:59:59.999 have a rough idea of what it means 99:59:59.999 --> 99:59:59.999 so 99:59:59.999 --> 99:59:59.999 let's jump straight into the first... 99:59:59.999 --> 99:59:59.999 oh wait, sorry. before that 99:59:59.999 --> 99:59:59.999 i just want to make it clear that 99:59:59.999 --> 99:59:59.999 in common use right now 99:59:59.999 --> 99:59:59.999 there are two main python versions 99:59:59.999 --> 99:59:59.999 so python 2 and python 3 99:59:59.999 --> 99:59:59.999 there are some differences in these 2 versions 99:59:59.999 --> 99:59:59.999 so as beginners 99:59:59.999 --> 99:59:59.999 it can be easy to trip up 99:59:59.999 --> 99:59:59.999 over these differences 99:59:59.999 --> 99:59:59.999 so just to be clear that 99:59:59.999 --> 99:59:59.999 when you read up resources online 99:59:59.999 --> 99:59:59.999 for example, you should know 99:59:59.999 --> 99:59:59.999 what the version 99:59:59.999 --> 99:59:59.999 those resources are talking about 99:59:59.999 --> 99:59:59.999 which python version they are talking about 99:59:59.999 --> 99:59:59.999 so for clarification 99:59:59.999 --> 99:59:59.999 just be clear 99:59:59.999 --> 99:59:59.999 I am using python 3 for all my examples here 99:59:59.999 --> 99:59:59.999 alright 99:59:59.999 --> 99:59:59.999 so objects 99:59:59.999 --> 99:59:59.999 think of objects as a way of 99:59:59.999 --> 99:59:59.999 logically encapsulating your code 99:59:59.999 --> 99:59:59.999 so objects has what we called attributes 99:59:59.999 --> 99:59:59.999 and methods 99:59:59.999 --> 99:59:59.999 so let's look at this doggeh over here 99:59:59.999 --> 99:59:59.999 i like to think of attributes as 99:59:59.999 --> 99:59:59.999 kinda like 99:59:59.999 --> 99:59:59.999 adjectives? 99:59:59.999 --> 99:59:59.999 or properties 99:59:59.999 --> 99:59:59.999 So for example you say 99:59:59.999 --> 99:59:59.999 the dog is white 99:59:59.999 --> 99:59:59.999 or can have a weight of 7 kg 99:59:59.999 --> 99:59:59.999 so these are like the attributes of the dog 99:59:59.999 --> 99:59:59.999 on the other hand 99:59:59.999 --> 99:59:59.999 methods 99:59:59.999 --> 99:59:59.999 they are kinda more like verbs 99:59:59.999 --> 99:59:59.999 a dog can wag tail and bark 99:59:59.999 --> 99:59:59.999 so these are the things 99:59:59.999 --> 99:59:59.999 the dog can do 99:59:59.999 --> 99:59:59.999 these are like 99:59:59.999 --> 99:59:59.999 methods of my dog object 99:59:59.999 --> 99:59:59.999 this is a slightly lame description 99:59:59.999 --> 99:59:59.999 but i hope can bring across 99:59:59.999 --> 99:59:59.999 into the field of what 99:59:59.999 --> 99:59:59.999 I mean by attributes and method 99:59:59.999 --> 99:59:59.999 and in fact, you know 99:59:59.999 --> 99:59:59.999 in python 99:59:59.999 --> 99:59:59.999 you have your variables and your functions 99:59:59.999 --> 99:59:59.999 right? 99:59:59.999 --> 99:59:59.999 so your attributes and methods 99:59:59.999 --> 99:59:59.999 are just your good ol' friends 99:59:59.999 --> 99:59:59.999 your variables and functions 99:59:59.999 --> 99:59:59.999 the only thing is that 99:59:59.999 --> 99:59:59.999 attributes are kind of like variables 99:59:59.999 --> 99:59:59.999 that are associated with a certain object 99:59:59.999 --> 99:59:59.999 and methods are functions 99:59:59.999 --> 99:59:59.999 that are associated with a certain object 99:59:59.999 --> 99:59:59.999 in the end, 99:59:59.999 --> 99:59:59.999 objects basically is a way of calculating data 99:59:59.999 --> 99:59:59.999 and a form of attributes 99:59:59.999 --> 99:59:59.999 with functions of procedures 99:59:59.999 --> 99:59:59.999 in the form of methods 99:59:59.999 --> 99:59:59.999 Okay 99:59:59.999 --> 99:59:59.999 so let's look at this dog transcribe into 99:59:59.999 --> 99:59:59.999 real python code 99:59:59.999 --> 99:59:59.999 for those who have not done any OOP in python before 99:59:59.999 --> 99:59:59.999 this syntax might look intimidating but 99:59:59.999 --> 99:59:59.999 hopefully by the end of it 99:59:59.999 --> 99:59:59.999 you will be able to understand 99:59:59.999 --> 99:59:59.999 what's going on 99:59:59.999 --> 99:59:59.999 The kind of object-oriented programming 99:59:59.999 --> 99:59:59.999 in python 99:59:59.999 --> 99:59:59.999 that we are dealing with 99:59:59.999 --> 99:59:59.999 is called class-based object oriented 99:59:59.999 --> 99:59:59.999 programming 99:59:59.999 --> 99:59:59.999 This is the kind of OOP 99:59:59.999 --> 99:59:59.999 you will find 99:59:59.999 --> 99:59:59.999 in languages like Ruby, Java, C# and etc. 99:59:59.999 --> 99:59:59.999 Only the most popular kind of OOP 99:59:59.999 --> 99:59:59.999 Just so you know, 99:59:59.999 --> 99:59:59.999 there are other kinds out there 99:59:59.999 --> 99:59:59.999 Class-based OOP 99:59:59.999 --> 99:59:59.999 you see here that 99:59:59.999 --> 99:59:59.999 I am defining this class "dog" 99:59:59.999 --> 99:59:59.999 you should know by now 99:59:59.999 --> 99:59:59.999 that in python, 99:59:59.999 --> 99:59:59.999 indentation is intactly(?) significant 99:59:59.999 --> 99:59:59.999 so all these indented code 99:59:59.999 --> 99:59:59.999 this is inside the class body. 99:59:59.999 --> 99:59:59.999 This is my class definition. 99:59:59.999 --> 99:59:59.999 Now I am talking about classes 99:59:59.999 --> 99:59:59.999 Now I have to talk about 99:59:59.999 --> 99:59:59.999 like explain about 99:59:59.999 --> 99:59:59.999 classes and instances 99:59:59.999 --> 99:59:59.999 Because we are talking about class-based OOP 99:59:59.999 --> 99:59:59.999 so what are classes and instances? 99:59:59.999 --> 99:59:59.999 So, think of a class as a kind of category 99:59:59.999 --> 99:59:59.999 where an instance is 99:59:59.999 --> 99:59:59.999 particular living example 99:59:59.999 --> 99:59:59.999 of a kind of thing 99:59:59.999 --> 99:59:59.999 To drive the point across, 99:59:59.999 --> 99:59:59.999 let me talk about babies 99:59:59.999 --> 99:59:59.999 Yes! Babies 99:59:59.999 --> 99:59:59.999 When you think of 99:59:59.999 --> 99:59:59.999 a baby right.. 99:59:59.999 --> 99:59:59.999 a human baby 99:59:59.999 --> 99:59:59.999 you kind of have this conception 99:59:59.999 --> 99:59:59.999 tectonic idea of what babies 99:59:59.999 --> 99:59:59.999 should look like 99:59:59.999 --> 99:59:59.999 sweet, tender, angel 99:59:59.999 --> 99:59:59.999 angelic things 99:59:59.999 --> 99:59:59.999 That is sort of like class 99:59:59.999 --> 99:59:59.999 this general category 99:59:59.999 --> 99:59:59.999 this general idea 99:59:59.999 --> 99:59:59.999 whereas if you actually 99:59:59.999 --> 99:59:59.999 see this real living kicking screaming 99:59:59.999 --> 99:59:59.999 pooping baby 99:59:59.999 --> 99:59:59.999 you are like 99:59:59.999 --> 99:59:59.999 "What's this ugly yoda-like thing?" 99:59:59.999 --> 99:59:59.999 That is kind of like instance 99:59:59.999 --> 99:59:59.999 that's the real concrete 99:59:59.999 --> 99:59:59.999 instance of a baby 99:59:59.999 --> 99:59:59.999 So I hope that kinda brings across the idea 99:59:59.999 --> 99:59:59.999 of what's the differences between 99:59:59.999 --> 99:59:59.999 the class and instance 99:59:59.999 --> 99:59:59.999 Let's go back to the code 99:59:59.999 --> 99:59:59.999 and in my class 99:59:59.999 --> 99:59:59.999 so in this class definition right 99:59:59.999 --> 99:59:59.999 when I define my class 99:59:59.999 --> 99:59:59.999 you can think of it as 99:59:59.999 --> 99:59:59.999 defining a custom data type 99:59:59.999 --> 99:59:59.999 so you know in python 99:59:59.999 --> 99:59:59.999 we have our built-in types 99:59:59.999 --> 99:59:59.999 we have integers 99:59:59.999 --> 99:59:59.999 we have strings 99:59:59.999 --> 99:59:59.999 we have lists 99:59:59.999 --> 99:59:59.999 we have dictionaries 99:59:59.999 --> 99:59:59.999 When you define your class 99:59:59.999 --> 99:59:59.999 it is as if you are building 99:59:59.999 --> 99:59:59.999 your own data type 99:59:59.999 --> 99:59:59.999 And this one data type 99:59:59.999 --> 99:59:59.999 has its own attributes and 99:59:59.999 --> 99:59:59.999 its own methods 99:59:59.999 --> 99:59:59.999 I will go through the stuff here later 99:59:59.999 --> 99:59:59.999 but let me go down to the code here 99:59:59.999 --> 99:59:59.999 so what you see down here 99:59:59.999 --> 99:59:59.999 in this line, line 13 99:59:59.999 --> 99:59:59.999 this is where I instantiate 99:59:59.999 --> 99:59:59.999 I create an instance for my class 99:59:59.999 --> 99:59:59.999 So I am assigning 99:59:59.999 --> 99:59:59.999 I am creating 99:59:59.999 --> 99:59:59.999 an instance called "Doggeh" 99:59:59.999 --> 99:59:59.999 And this the way I construct 99:59:59.999 --> 99:59:59.999 my instance 99:59:59.999 --> 99:59:59.999 by calling the, what we called 99:59:59.999 --> 99:59:59.999 the class constructor 99:59:59.999 --> 99:59:59.999 So this is the class name 99:59:59.999 --> 99:59:59.999 the parenthesis 99:59:59.999 --> 99:59:59.999 I pass in certain arguements 99:59:59.999 --> 99:59:59.999 to initialise my instance 99:59:59.999 --> 99:59:59.999 so now I have this Doggeh 99:59:59.999 --> 99:59:59.999 which is an instance of a dog class 99:59:59.999 --> 99:59:59.999 Okay 99:59:59.999 --> 99:59:59.999 so now that we sort of know 99:59:59.999 --> 99:59:59.999 what classes and instances are 99:59:59.999 --> 99:59:59.999 let me go back 99:59:59.999 --> 99:59:59.999 I will start going through the code here 99:59:59.999 --> 99:59:59.999 So to understand the code in here 99:59:59.999 --> 99:59:59.999 we have to talk about 99:59:59.999 --> 99:59:59.999 attributes and methods 99:59:59.999 --> 99:59:59.999 When we talk about attributes and methods 99:59:59.999 --> 99:59:59.999 There are two kinds 99:59:59.999 --> 99:59:59.999 They can be instance attributes or instance methods 99:59:59.999 --> 99:59:59.999 or they can be class attributes and 99:59:59.999 --> 99:59:59.999 class methods 99:59:59.999 --> 99:59:59.999 But for now 99:59:59.999 --> 99:59:59.999 I will talking about instance attributes 99:59:59.999 --> 99:59:59.999 and instance methods 99:59:59.999 --> 99:59:59.999 Because that is what we usually deal with 99:59:59.999 --> 99:59:59.999 Inside this class body 99:59:59.999 --> 99:59:59.999 I define several methods 99:59:59.999 --> 99:59:59.999 for my dog class 99:59:59.999 --> 99:59:59.999 so these are how you define functions 99:59:59.999 --> 99:59:59.999 I mean, methods are just functions 99:59:59.999 --> 99:59:59.999 but they are inside a class 99:59:59.999 --> 99:59:59.999 This is how I define my methods 99:59:59.999 --> 99:59:59.999 And 99:59:59.999 --> 99:59:59.999 you notice that 99:59:59.999 --> 99:59:59.999 this first method looks kind of special 99:59:59.999 --> 99:59:59.999 with the double underscore in front 99:59:59.999 --> 99:59:59.999 So double underscore init double underscore 99:59:59.999 --> 99:59:59.999 so i will be talking more about this later 99:59:59.999 --> 99:59:59.999 but let's look at these two 99:59:59.999 --> 99:59:59.999 func.. methods first 99:59:59.999 --> 99:59:59.999 Now you will notice something interesting 99:59:59.999 --> 99:59:59.999 about this method 99:59:59.999 --> 99:59:59.999 I mean, in the end, 99:59:59.999 --> 99:59:59.999 these methods are just stuff that 99:59:59.999 --> 99:59:59.999 they print something to the console 99:59:59.999 --> 99:59:59.999 right 99:59:59.999 --> 99:59:59.999 It is going to print "Wag Wag" or 99:59:59.999 --> 99:59:59.999 "Woof Woof" 99:59:59.999 --> 99:59:59.999 but there is something interesting about this 99:59:59.999 --> 99:59:59.999 in the method signature 99:59:59.999 --> 99:59:59.999 I am actually taking this parameter 'self' 99:59:59.999 --> 99:59:59.999 and 99:59:59.999 --> 99:59:59.999 this 'self' is actually an indication 99:59:59.999 --> 99:59:59.999 to you that 99:59:59.999 --> 99:59:59.999 these methods are instance methods 99:59:59.999 --> 99:59:59.999 We can invoke these instance methods 99:59:59.999 --> 99:59:59.999 by using this Dog syntax over here 99:59:59.999 --> 99:59:59.999 so I invoke "wag tail" by 99:59:59.999 --> 99:59:59.999 calling it Doggeh.wag_tail() 99:59:59.999 --> 99:59:59.999 And just to emphasize that you have 99:59:59.999 --> 99:59:59.999 to put the parenthesis there 99:59:59.999 --> 99:59:59.999 to invoke the method 99:59:59.999 --> 99:59:59.999