 So it's one-two pythons flying circus but okay actually it's not but just so you know this is where the python language got its name it's from the comedy truth not the snake Okay but today's talk will be about OOP or Object Oriented Programming in python and I'm Chinsi I'm a 2nd year student from the Singapore University of Technology and Design currently in the Computer Science Slash Control Systems major and I'm also currently an intern at Tickerbox and in fact it is my fellow intern who invited me to speak here today so I'm glad to be here so now I understand that most of the audience here are at the beginner level so just to get engaged how many of you guys have done OOP and python before? okay so I would say that for those who have not done anything I hope that at the end of this talk we get to understand more about what OOP is about okay so I would say OOP is like an intermediate level topic but it's a very key topic for you to understand and you want to do more advanced stuff so this is why I chose the topic today but it's also a very broad topic so I don't know how much I can cover in 20 minutes so forgive me if I speak too fast or I'll try to skip over certain things okay so just to agree with you on what I'll be talking about I'll mainly be covering more like nuts and bolts of OOP and python so language specific constructs syntax that's all the thing because I believe for beginners this is what you would be more interested in but at the same time I hope to be able to talk a little bit about what you call object oriented design principles of writing good code with OOP with her yeah, design principles basically so before I actually go to actual material I have a confession to make so the sad truth is okay maybe it's not that sad but the truth is that I probably enjoy drawing silly things more than I like programming so as a result you will see a lot of silly drawings probably more silly drawings than code in this talk but it is my hope that oh dear it is my hope that it will sort of give you a visual aid to understanding the concepts I'll be talking about okay so let's dive right in the nuts and bolts so these are the topics I'll be covering objects, classes and instances attributes and methods, inheritance and scope and if those words have completely thrown over your head do not fear by the end of this talk you should hopefully have rough idea of what they mean so let's jump straight into the first oh wait, sorry before that I just want to make it clear that in common use right now there are two main titan versions so titan 2 and titan 3 there are some differences between the two versions so as beginners it can be easy to trip up over these differences so I just just to be clear that when you read resources online for example you should know what versions those resources are talking about which titan version you're talking about so for clarification just to be clear I'm using titan 3 for all my examples here alright so objects so think of objects as a way of logically encapsulating your quote so objects have what we call attributes and methods so let's look at this this doggie over here so I like to think of attributes as kind of like adjectives or properties so for example you say a dog is white or you can have a weight of 7 kg so these are like attributes of a dog on the other hand methods they kind of more like verbs so a dog can wag, tail, can bark so these are things that a dog can do so these are like the methods of my dog object so you know in Python so this is a slightly lay managed description but I hope can kind of bring across into a diffial of what I mean by attributes and methods and in fact you know in Python you have your variables and your functions right so your attributes and methods are actually just your good old friends your variables and your functions the only thing is that attributes are kind of like variables that are associated with a certain object and methods are functions that are associated with a certain object so in the end objects objects basically aware of coupling data in the form of attributes with functions or procedures in the form of methods okay so let's look at this dog transcribed into real Python code so for those who have not done any OOP in Python before this the syntax might look subtly intimidating but hopefully at the end you will be able understand what's going on so the kind of object oriented programming in Python or class based object oriented programming right but this is the kind of OOP that you'll find in languages like Ruby, Java, C sharp et cetera and it's probably the most popular kind of OOP just so you know there are other kinds of there so class based OOP so you see here that I'm defining this class dog so you should know why not that in Python indentation is syntactically significant so all this indented code this is inside the class body okay so this is my class definition okay now I'm talking about classes now I have to talk about explain about classes and instances because we're talking about class based OOP so what are classes and instances so think of a class as a kind of thing category okay whereas an instance is more like this particular living example of the kind of thing so you know to try to point a class let me talk about babies yes yes babies okay many think of a baby a human baby you kind of have this conception this ketonic idea of what a baby should look like this sweet tender angelic thing so that's kind of like a class it's like this general category this general idea whereas if you actually see this real living kicking screaming peeing pooping baby and you're like what's this ugly Yoda like thing that's kind of like an instance that's a real concrete instance of a baby okay so I hope that kind of brings across the idea of what's the difference between a class and an instance okay so let's go back to the code and in my class so in this class definition when I define my class you can think of it as defining a custom data type so we know in Python we have our built-in types we have integers we have strings we have lists we have dictionaries when you define a class right and this own data type it has its own attributes and its own methods okay I will go through the stuff in here later but let me go down to the code here so what you see down here this line 930 this is where I instantiate I create instance of my class so I am assigning and creating instance called doge and this is the where I construct the instance by calling the we call the class constructor so this is the class name what we call I pass in certain arguments to initialize my instance so now I have this doge doge which is an instance of the doge class okay so now that we sort of know what classes and instances are let me go back I will start going through the code in here so to to understand the code in here we have to talk about attributes and methods so we talk about attributes and methods right there are generally two kinds there can be instance attributes and instance methods or there can be class attributes and class methods but for now I will be talking about instance attributes and instance methods because that is what we usually deal with okay so inside this class body I have defined several methods for my doge class so these are just like how you define functions I mean methods are just functions right but they are inside a class so this is how I define my methods and you notice that you notice that this first method looks kind of special as the double underscore so double underscore init double underscore so I will be talking more about this later but let's look at these two these two methods first now you will notice something interesting about these two methods I mean in the end these methods are just stuff that they print something to the console just going to print but there is something interesting about this in that in the method signature I am actually taking in this parameter self and this self is actually an indication to you that these methods are instance methods okay so we can invoke these instance methods by using this dot syntax over here so I invoke web till by calling doge.weg till and this is and it just be just to emphasise that you have to put the parenthesis there to invoke the method right in python putting the parenthesis there as important so when I invoke the method I use this dot syntax and I write the method name and I put the parenthesis so when I invoke these methods they will print web-web okay so let me talk a little bit more about that self parameter why is it there you notice it is slightly different from normal functions because in a normal function when you define a function you define it with a set of parameters it takes like say 3 parameters when you invoke that function you should have to pass in 3 arguments so in this case I take 1st, 2nd and 3rd parameters and when I invoke this function I pass in 3 arguments 3 parameters, 3 arguments so that's how it normally works but if you notice here when I invoke these function these methods even though I'm taking in this self parameter somehow I'm not passing anything in right that seems a bit strange so for me to for you guys to understand what's going on I need to talk a bit more about this this magical self what is this about so when we at the end of this you actually realize that the self is not magical the magic is not in the self okay the thing is when you when you call these methods invoke these methods like this what python actually does is under the hood it's passing in to the method the instance that calls the method so for example when I call doggit out web kill it's passing in this doggit instance into my web kill method and it's passing that that instance into the self parameter so in fact my self parameter is actually referencing this doggit instance and at this point it may not be obvious because in my methods I'm not doing anything with the self I'm not doing anything with it but I'll modify the method later to show you how I can actually access that self parameter but for now I realize that these methods they're actually what we call bound methods with python so there are methods that are bound to a specific instance okay so this method this web kill method so notice I'm printing this method out I'm not putting the parenthesis behind so I'm not calling the method this is the method object but in python everything's an object so even methods are objects so I'm printing out this method and it tells me that it's actually a bound method right is this bound method dog dog web kill of this particular instance of dog so let's modify the bug method so that I'm doing something more interesting itself so you now see here that I'm actually accessing this self dog color self dog web so I'm accessing these attributes that belong to that instance and now instead of printing just wolf wolf it will print like wolf wolf I'm white and I weigh 7kg because white because when I initialize this dog egg I pass in white I initialize it with a white color and I initialize it with 7kg of weight okay so this is what you can do with that self parameter so if you've come from a language like say ruby you might be inclined to think so this is the equivalent code in ruby and it does the exact same thing and in ruby you can actually call this you can also do this self dog color self dog weight thing but I want to say that the self in ruby and the self in python are very different so in ruby the self is actually a keyword it's good into the language in python let's see what happens if so originally I had this this bug method let's see what happens if I do this you know what it still works okay so it's not the self that's magical the magical thing is the first parameter that the method accepts I can name it whatever I want and it will work so you can name it the most ridiculous things you want and it will work okay but the only reason we call it self is because of python convention it's a very strong convention so you're not going to see code like this okay no one's going to write code like this so when people write code like this they'll call me trolls okay but I'm I'm here to teach you to write python not to be a troll so you know if you write code like this people are going to look at you like that you don't want that okay so just the upshot end of the day is make sure yourself is there yourself should be that self parameter should always be there for all your instance methods okay what if what if I don't define that self so you see what happens python actually trolls an error because python is still passing in the instance into the method but you're saying your method doesn't take any heritators so it gets an error okay alright so now I've I'm done talking about instance methods so let me talk about instance attributes so this is where I come back to this first interesting method over here so the init method is what we call the constructor so the constructor is the thing that initializes our class so when you when I created this dogged instance I initialized it with this data right white color 7kg and this is where I actually set my data so remember like I said self refers to the instance self dogged color refers to the color of the instance so I'm setting the self dogged color to the color that pass into the constructor and when you see this double underscore thing going on in python it's what we call magic error magic method so python has quite a long list of this but the init method is probably the most common because it's the class constructor and so you notice for attributes I can also read learn through dogged so I can print the dogged color and I can also assign to them through dot syntax so I can assign dogged dot color goes to brown and when I print the color out again now I print brown instead of white okay now that I'm done talking about instance attributes instance methods what's left next is class attributes and class methods so what's the difference so the main thing is that when we talk about instance attributes and methods we are talking about data procedures and deal with each dot can bug but there are certain kind of data that is class white it's not specific to one particular dot so those kind of data will make sense to store in a class attribute for example okay so what kind of attribute would that be so how about the threshold weight over which a dot is considered obese okay that is not specific to any one dot it's a class white thing so this is something that will make sense to store as a class attribute and it is it's an important thing to know right okay he agrees okay so that's this is how we initialise dot class attribute okay it's just like assigning a normal variable but this is inside the class body so I've created this obese weight class attribute set it to 30 and so I have this instance method here called check obese and I can access this class attribute within my instance method through this so self remember self again is just the instance self dot class refers right it refers to the class to which that instance belongs in this case it's the dot class so this is as good as writing dot dot obese weight but you shouldn't hard code dot dot obese weight directly because that will make your code less maintainable because let's say one day you want to change the class name then you have to hunt down all the places where you hard coded the name in so this way you always should reference it true self dot class okay that's about it let me talk a bit about class methods so this is how the syntax of writing class method okay you have to write this at class method thing so this is what we call a decorator you have this at class method decorator and then you define a method so just like how your instance methods took in the first parameter self your class methods will take in this first parameter class or CRS right it's the same thing so you call instance methods on an instance right you call class methods on the class so here when I call my class method and calling it on dot big dot dot dot info this dot is my class and you see what my class method is doing is basically kind of printing information that has to do with a class not with any specific instance of alright now I shall try to talk about inheritance this is kind of a really big topic in programming so let's look at this small family tree here when we talk about inheritance we tend to talk about what we call an is a relationship okay so a chiwawa is a dog a guanis mountain dog is a dog so the point of inheritance is to abstract out the commonalities of similar things and to put it in what we call the base of the parent class so this dog is my base of parent class and I have I can create these child subclasses that will extend the parent class and they will add on their own stuff to the parent class so for example we have many different dog breeds and it doesn't make sense for all dog breeds to have the same obese weight threshold so we can create these child classes that specify the obese weight for that specific dog breed so in my parent class remember I specified the obese weight to be 30 but obviously chiwawa if they are over 5 then it's problematic already so I can overwrite the parent class attribute over here and specify that for chiwawa your overweight your over 5 kg but for guanis mountain dogs I mean those are huge dogs your overweight your over 55 kg so that's kind of like what you can do with inheritance and notice that I'm not specifying anything else in my class I'm just specifying this obese weight thing and yet when I instantiate this chiwawa here my chiwawa still can bark and this barking it came from the parent dog class because all dogs can bark so whether you're chiwawa or guanis mountain dog you should still be able to bark you should still be able to check whether you're obese this is stuff that is inherited from the parent okay, not only can you overwrite attributes you can also overwrite methods so for example chiwawa I mean they don't really bark they just can't speak so if I want to overwrite the bark method sorry my parent dog class I can basically just define a method with the exact same signature right so and in this case it does something different instead of printing wolf wolf so this is how I overwrite methods in my parent class and now you might be thinking okay sometimes I want to overwrite methods but sometimes I still want some functionality from the parent class and I want to add on more of my own stuff so this is what we call extending the parent parent class methods so you can do that but first invoking the parent method and then adding on your own stuff so for example let's say chiwawa you have chiwawa you have long fur you have chiwawa you have short fur so when you initialize a chiwawa you not only want one to know its age its weight and its colour but you also want to know its hair length but in the parent dog class we only initialize the age and weight and the colour so what you can do is you can actually invoke the parent class constructor and this is what this super line is doing here so this super dot in it colour age weight this is calling the parent class contractor and the parent class constructor only deals with colour age and weight and then after that I add on whatever class specific stuff so for chiwawa I want to set the hair length as well so this is how we can extend parent methods okay so this is my last topic almost there so scope a bit more about some object-oriented design principles okay scope so when we talk about object-oriented we talk about interactions between objects we tend to talk about in terms of what we call message-passing okay so when when objects communicate they pass messages and they pass messages by calling methods on other objects so for example this dot I can send a wetfield message to this dot and you will wet it still so the only the messages that I should be able to send to this dot belong to its public interface okay the dot presents an interface that you can interact with but the dot also has its own internals that only itself knows like you shouldn't be able to touch it so imagine right imagine if to get a dot to wet its tail you know instead of saying nicely to it hey please wet your tail no I actually when it grabs brain try to stimulate the motor nerve directly I mean you don't do that right it's like you're like monesting the dot okay so it's the same thing when you write code you try not to touch the internals you try to you try to think of how you're supposed to design a public interface that the object presents to everything else so for example when you when you tell the dot hey wet your tail the dot's brain will take that information process it and at the end of the day eventually it will wet its tail but however you don't want to care you don't want to care okay so that's the idea even when you write your code or when you use other people's code and you use libraries for example the libraries present an API to you that you have learnt how to use you don't have to care about how the internals of the library will really works okay unless you know you're actually contributing to the library it's a different thing okay now real code this will be a bit brief so the way we can scope our method methods attributes the way we can declare whether something is part of the public interface or whether something is considered private and Python it's purely through how we name our attributes or methods so for example here I've actually defined this private method and how I know it's private it's purely by the name because this name has this double underscore in front right so I can only call private methods from within the class itself so I can call this tail speed method within my wet tail method so in this case self dot tail speed I'm invoking this tail speed method okay but if I try to call tail speed outside and directly if I to invoke it here Python is going to show an error it's going to say it doesn't exist okay because you shouldn't be able to touch private methods okay but so it looks like your method is well protected here but the truth is there's no real privacy if you really really wanted to you could actually still go and touch that method I won't go through as a few how you can do it now but the truth is that this kind of privacy the only thing that's enforcing this kind of privacy is convention so okay I'll skip that so if we see methods or attributes with this kind of name don't touch them like don't mess around with them that's the idea it's purely by convention that people say if I write if I name my stuff like that they are private okay so just just to bring across the point so imagine you're like this annoying developer kid right you see this method it's an underspun in front of like ooh what does this method do when you go and try to poke it understand that there's a reason that underscores in front okay so you have been warned and if if you've been warned and you still want to play it then you'll get hurt so nothing good really comes about from violating this kind of strong conventions like your quote will get messy trying to and yeah it's kind of hard to bring across the idea now but it's something that you experience when you start writing okay so and people will look at you like that it's not fun okay i'm about done so just to wrap up because i understand that dina is going to be talking to you guys about Django i'll just show you some real quote from Django and a disclaimer i've not touched Django at all before so this is blatantly stolen from the Django documentation but i just want to show you that with this basic understanding of object oriented programming it's not that hard to understand how this kind of quote works so even for me i've never touched Django before i can sort of understand how this works so this model view controller web framework so models are a way of storing our data so you see that these are just classes class reporter class article and these classes they inherit from Django's models.model and this models.model is imported from the models model of the Django library okay so how we store the model fields right model store data their fields so we store them as class attributes so this full name is a class attribute and this class attribute is assigned an instance of all these field classes so these character field, dead field text field right i'm actually instantiating i'm actually creating instances of these classes over here and i'm assigning them to the class attribute of the model and not only can we assign class attributes we can also define instance methods so this underscore string is another magic method in Titan which allows you to define the string representation of your object so when i print out a reporter object the full name of the reporter instead of the default which is like which is like the memory address of the object and that's about all thank you should we be hanging out after the event so we can also ask her if you have any questions or if you have any questions that you