Python OOP Tutorial 1: Classes and Instances

Python OOP Tutorial 1: Classes and Instances
Spread the love

hey everybody how’s it going in this  series of videos we’ll be learning how  to create and use classes within Python  and how object-oriented concepts are  applied within the language now there’s  a lot to cover when working with classes  so I’m going to break these up into  several different videos we’ll cover the  basics of creating and instantiating  classes will learn about inheritance  class and instance variables static  methods and class methods and several  other topics so breaking these up in  several videos will allow us to focus on  specific topics in each video so in this  video we’ll be learning the basics of  creating and instantiating simple  classes but first why should we even use  classes now this isn’t just specific to  Python you can see classes being used  throughout most modern programming  languages and there’s a good reason for  that they allow us to logically group  our data and functions in a way that’s  easy to reuse and also easy to build  upon if need be now just a quick side  note when I say data and functions that  are associated with a specific class we  call those attributes and methods and  you’ll hear me use those terms a lot  throughout these videos so when I say  methods I mean a function that is  associated with a class so let’s go  ahead and get started  so say we had an application for our  company and we wanted to represent our  employees and our Python code now this  would be a great use case for a class  because each individual employee is  going to have specific attributes and  methods so for example each employee is  going to have a name and email address a  pay and also actions that they can  perform so it’d be nice if we had a  class that we could use as a blueprint  to create each employee so that we  didn’t have to do this manually each  time from scratch so let’s go ahead and  create a simple employee class and see  what that would look like so to create a  class it’s just as easy as saying class  employee now I’m going to leave this  empty for now and if we just left it  like this and we get an error so if you  ever have a class or a function that you  want to leave empty for the time being  then you can simply put in a past  statement and Python will know that you  just want to skip that for now so now we  have a simple employee class with no  attributes or methods and I wanted to  stop here because I wanted to explain  the difference between a class and an  instance of a class  so our class is basically a blueprint  for creating instances and each unique  employee that we create using our  employee class will be an instance of  that class so for example if I said  employee 1 equals employee and employee  2 equals employee then each of these are  going to be their own unique instances  of the employee class so for example if  I go ahead and print both of these out  and copy and paste that there and you  can see that both of these are employee  objects and they’re both unique they  both have different locations here in  memory now this is an important  distinction because you’ll hear me talk  a lot about instance variables and class  variables and it’s important to know the  difference between those and I’ll go  more in depth into class variables in  the next video but for this video we’re  going to be looking at instance  variables so instance variables contain  data that is unique to each instance now  we could manually create instance  variables for each employee by doing  something like this so let’s say we  wanted employee 1 to have a first name  and a last name so I could just do  employee 1 not first equals Korie and  then I could do employee last is equal  to Schaefer and I could also give it an  email address so I’ll do employee 1 dot  a mail equals and I’ll just do the first  name with the last name at company comm  and lastly let’s go ahead and add a pay  onto there too so I’ll do employee 1 not  pay and we’ll just do 50,000 there now  let’s give employee to some of these  same attributes so for this one I’ll  just do test user and then I’ll do test  user and I will make that 60,000 ok so  now each of these instances have  attributes that are unique to them so if  I print it out let’s say I could print  out the employee 1 dot mail and  also print out the employee to email on  it looks like whenever I gave these the  instance variables I forgot to make this  employee to here so now let’s run that  so now you can see that that email was  created for each employee let’s say that  we wanted to set all of this information  for each employee when they’re created  rather than doing all of this manually  like we did here so we wouldn’t want to  have to manually set these variables  every time you can see it’s a lot of  code and it’s also prone to mistakes  just like I did whenever I forgot to  change the to employee too so we don’t  get much benefit of using classes if we  did it this way so to make these set up  automatically when we create the  employee we’re going to use a special  and knit method so now inside of our  employee class I’m going to create this  special knit method now you can think of  this method as initialized and if you’re  coming from another language then you  can think of this as the constructor now  when we create methods within a class  they receive the instance as the first  argument automatically and by convention  we should call the instance self now you  can call it whatever you want but it’s a  good idea to stick to convention here  and just use self so after self we can  specify what other arguments that we  want to accept so let’s go ahead and  accept the first name the last name and  the pay and I know that we had email too  but we can create the email using the  first name and the last name so now  within our and knit method we’re going  to set all of these instance variables  so let’s do self dot first equals first  and now I’m just going to do this for  the rest of them as well so I’m going to  do self dot pay or self dot last and  self dot pay and for the email I can do  self dot email equals first plus and  then we’ll put a dot between those and  then last and then we’ll add on to the  end at company comm okay so whenever I  say that self is the instance what I  mean by that is that when we  self dot first equals two first here  it’s going to be the same thing as us  saying down here that employee one dot  first equals Korey except now instead of  doing this manually it’ll be done  automatically when we create our  employee objects now these don’t need to  be the same as our arguments so for  example I could make this self dot F  name equals first but I usually like to  keep these similar if possible so I’m  going to go ahead and set that back to  self dot first equals first okay so now  when we create our instances of our  employee class right here now we can  pass in the values that we specified in  our anit method now our net method takes  the instance which we call itself and  the first name last name and pay as  arguments but when we create our  employee down here the instance is  passed automatically so we can leave off  self we only need to provide the names  and the pay so we could create these by  passing in first and we have to do this  in order so I will pass in all of the  same information that we did manually  down there and for the second one I’ll  do test and user and I think I had that  at sixty thousand okay so what would  happen on this line when we create this  employee the anit method will be run  automatically so employee one will be  passed in as self and then it will set  all of these attributes so it’ll set  employee one dot first is equal to first  which we passed in as quarry employee  one that last equals what we passed in  is last and so on so now that we have  that an it method in place we can go  ahead and delete these manual  assignments that we made down here and  you can see by deleting that that we got  rid of a lot of code so I’m going to go  ahead and comment out those lines we’re  printing the employees and I’m just  going to go ahead and print out the  email so if I run that then you can see  that that still works okay so everything  that we have so far like the names and  email and the pay are all attributes of  our class  now let’s say that we wanted the ability  to perform some kind of action not to do  that we can add some methods to our  class so let’s say that I wanted the  ability to display the full name of an  employee now this is an action that  you’d likely need to do a lot with a  class like this so we can do this  manually outside the class if I was to  come down here and do print and I could  get the full name just by putting in two  placeholders there and doing a format  and saying employee one dot first and  employee one dot last and if I go ahead  and print this out and you can see that  we got the full name there but that’s a  lot to type in each time that you want  to display the employees full name so  instead let’s create a method within our  class that allows us to put this  functionality in one place so within our  class here I’m going to create a method  called full name and we can do that with  just doing a death of full name now like  I said before each method within a class  automatically takes the instance as the  first argument and we’re going to always  call that self and the instance is the  only argument that we’ll need in order  to get the full name so within this  method here I’m just going to take the  same logic that we had down here and cut  that out and I’m just going to go ahead  and return that but we have to be  careful here because now instead of  printing employee 1 first-name and  lastname I’m going to use self so that  it will work with all instances so I’m  going to do self dot first and self dot  last so now that we created that method  instead of printing like we did before  now I can just come down here and do  employee 1 dot full name and print that  out and if I run that then you can see  that we got the same thing and notice  that we need the parentheses here  because this is a method instead of an  attribute so if I left the parentheses  off and printed this then you can see  that it prints the method instead of the  return value of the  method so we’re going to have to put  those parentheses on in order to run  that properly so now we have full  advantage of code reviews here so  instead of typing this out for each full  name that I want to print now I can just  use that method so now if I wanted to  print employee two’s full name it’s just  as easy as replacing employee one with  employee two and running that and we get  the correct answer okay and one more  quick thing that I wanted to point out  here now one common mistake that I see  when creating methods is forgetting the  self argument for the instance so let’s  take a quick look at what that would  look like if we left that off so now  before I run this if I just comment it  out this printing of full name down here  and ran this then you can see I’m  actually going to remove these print  statements here as well now you can see  that this runs without any errors but if  I was to try to run this method that we  accidentally left self off of then run  this and you can see that we get an  error and the error that we got was a  type error full-name takes zero  positional arguments but one was given  now this can be confusing because it  doesn’t look like we’re passing any  arguments here into full name but the  instance which in this case is employee  two is getting passed automatically so  we have to expect that instance argument  in our method and that’s why we added  self so I’m going to come back up here  to full name and put self back in and  now running this you can see that it  runs correctly now we can also run these  methods using the class name itself  which makes it a bit more obvious of  what’s going on in the background  because whenever we do that so I’ll do  employee dot full name now when we run  it from the class we have to manually  pass in the instance as an argument so  in this case I’ll pass in employee one  so you can see how these are similar but  not exactly the same so I’m going to put  these side-by-side just so that we can  compare them here so these two lines  here do the exact same thing but here  when I do employee one which is a  instance and I call the method I don’t  need to pass in self it does it  automatically and when we call the  method on the class and it doesn’t know  what instance that we want to run that  method with so we do have to pass in the  instance and that gets passed in as self  and if I go ahead and print this out and  run it then you can see that it works  just like if we were to print out the  employee one dot full name and I wanted  to show you that because that’s actually  what’s going on in the background when  we run employee one dot full name it  gets transformed into this here employee  dot full name and passes in employee one  as self and that’s why we have self for  these methods so I hope that makes sense  to you  that’s not extremely important to know  when we’re just getting started with  working with classes but we need to  understand that in later videos once we  start inheriting from other classes and  things like that so I figured it would  be a good thing to go ahead and show you  that now okay so I think that’s going to  do it for this video in this video we  learned how to create simple classes the  difference between a class and an  instance of that class and we also  learned how to initialize class  attributes and create methods now we  still have a lot to cover in future  videos and we’ll be going over more  advanced topics so in the next video  we’ll learn about class variables and  how they differ from instance variables  that we saw here but if you do have any  questions with what we covered in this  video then feel free to ask in the  comment section below and I’ll do my  best to answer those if you enjoy these  tutorials and would like to support them  then there are several ways you can do  that the easiest way is to simply like  the video and give it a thumbs up and  also it’s a huge help to share these  videos with anyone who you think would  find them useful and if you have the  means you can contribute through patreon  and there’s a link to that page in its  scription section below be sure to  subscribe for future videos and thank  you all for watching  

admin

Leave a Reply

Your email address will not be published. Required fields are marked *