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