Python OOP Tutorial 1: Classes and Instances

Hey, everybody. How’s[ it] going in this streaks of videos? We’ll be learning how to create and use categorizes within python and how object-oriented concepts are applied within the language now There’s a lot to cover when working with classifies, so I’m going to break these up into several different videos We’ll cover the basics of creating and instantiating categories will learn about endowment class and instance variables static methods and class methods and Several other topics So smashing 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 be used even use courses now this isn’t just specific to Python you can see first-class 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 exactly a speedy place 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 periods a lot throughout these videos, so when I say techniques I signify a function that is associated with a class[ so] let’s go ahead and get started so say we had an application for our firm And we wanted to[ represent] our employees and our python system 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] remunerate and likewise War 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 recognize what that would look like so to create a class It’s just as easy as saying class hire 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 government functions[ that] you want to leave empty for the time being then you can simply put In a past announcement and python will know that you exactly want to skip that for now So now we have a simple employee class with no attributes or approaches 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 Instance and each unique hire that we generate working our work class will be an instance of that class so for example if I said hire 1 equals work and EmPLoyee 2 Equals employee then each of these are[ proceeding] to be their own unique instances of the employee class so for example if I go ahead and publish both of these out and Copy and glue 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 speciman variables and class variables and it’s important to know the difference between those and I’ll vanish 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 all the employees 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 Corey and then I could do hire last is equal to Schaefer and I has the potential to commit it an email address, so I’ll do work 1 dot email equals and I’ll just do the given name with the last name at Company comm and lastly let’s go ahead and add a wage[ onto] there too, so I’ll do employee 1 not pay and we’ll just do 50,000 there now let’s pay hire to Some of these same qualities so for this one.I’ll simply do test User and then I’ll do measure User and I will conclude that 60,000 ok so now each of these instances have attributes that are unique to them so if I reproduce it out, let’s say I could engrave out government employees[ 1] dot mail and Likewise print out the employee to email On it looks like whenever I afforded these the instance variables I forgot to make this employee to now 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 the information collected for each employee When they’re initiated 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 misstep Just like I did whenever I forgot to change the[ to] hire,[ very] So we don’t get much welfare of the utilization of castes if we did it this practice, so to stimulate these Set up automatically when we create the employee we’re going to use a special init method So now inside of our work class I’m going to create this special init method Now you can think of this method as initialize and if you’re coming from another language Then you can think of this as the constructor now when we create techniques within a class they[ receive] the speciman as the first dispute Automatically and by convention.We should call the instance self now. You can call it Whatever you miss But it’s a good idea to stick to convention now and really use self So after self we can specify what other controversies that we want to Accept so let’s go ahead and accept the first name the[ last] refer and the pay and I know that we had email, more But we can create the email use the given 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 scatter first equals firstly and now I’m just going to do this for the rest of them as well So[ I’m] going to do now self fleck pay or self flecked last and self dot settle and for the email I can do self dot email equals firstly plus and then we’ll employ a fleck between those and then last and then we’ll add on to the end at companionship comm Okay, so whenever I say that self is the instance what I represent by[ that] is that when we self dot firstly? equals[ two] first here It’s going to be the same thing as us saying down now that hire one fleck first equals[ Korey] except now instead of doing this manually It’ll be done automatically when we create our hire objectives Now these don’t need to be the same as our reasons so for example.I could make this Self fleck f list equals firstly, but I usually like to keep these same if possible So I’m going to go ahead and name that back[ to] self dot first equals firstly, okay? So now when we create our the case of an our employee class right here now we can pass in the values that we specified in our anit method now our net approach makes the speciman which we call itself and the first name last name and Pay as arguments, but when we create our work down now the speciman is surpassed automatically So we can leave off self we only need to provide the specifies and the wage 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[ exam] and User and I speculate I had that at[ sixty][ thousand] okay so what would happen on this strand when we create this work the anit technique will[ be] led automatically, so Employee one will be overstepped in as ego and then it will specify all[ of] these attributes So it’ll pitch employee one scatter first is similar to first which we extended in as prey Employee one that last equals what we extended 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 works that we induced 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 indications[ we’re] engraving[ the] employees, and I’m just going[ to] go ahead and print out the email So if I race that then you can see that that still directs, okay? So everything that we have so far[ like] the appoints and email and the remuneration 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 means to our class So let’s say[ that] I missed the ability to display the full figure of an employee now This is an action that you’d likely need to do a good deal 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 mention precisely by position in two placeholders there and doing a format and saying hire one fleck first and Employee one fleck last and if I go ahead and print this out And you can see that we got the full identify there But that’s a lot to character in each time that you want to display the employees full list,[ so] instead Let’s create a method within our class that allows us to put this functionality in one place, so within our class now I’m going to create a method called full mention and we can do that with merely doing a death of Full mention now[ like] I said here each programme within a class Automatically makes the instance as the first arguing and We’re going to always announce[ that] soul and the speciman is the only argument that we’ll need in order to get the full specify So within this method now I’m just going[ to] go the same logic “that weve got” down here and cut that out and I’m just going to go ahead and return that but we have to be careful now 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 soul speck last-place So now that we created that programme instead of printing like we did before now I can just come down[ here] and do employee 1 dot full honour and Print that out And if I pass that then you can see that we got the same[ thing] and notice that we need the parentheses now Because this is a method instead of an facet So if I left the parentheses off and etched this then you can see that it publications the method instead[ of] the return quality of the method so we’re going to have to make those parentheses on in order to run that properly so now we have full advantage of Code scrutinizes here, so instead of typing this out for each full figure that I want to print now I can merely use that approach so now if I want to get etch employee two’s full reputation It’s just as easy as replacing hire one with work two and guiding that and we get the chastise answer Okay, and one more quick thing that I wanted[ to] point out here Now one common mistake that I see when creating techniques is forgetting the self assertion 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 time remark it out this publish of full identify down here and Ran this then you can see I’m actually going to remove these print statements now as well Now[ you] can see that this runs without any corrects But if I was to try to run this method that we inadvertently 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 makes zero positional proofs But one was given now this can be confusing because it doesn’t look like we’re passing any contentions here into full reputation But the instance which in this case is hire two is getting overtook automatically So we have to expect that speciman statement in our method, and that’s why we contributed self so I’m going to come back up[ now] to full appoint and Put self back in and now operating this you can see that[ it] runs Properly now we can also moved these methods working the class epithet Itself which manufactures it a bit more obvious of what’s going on in the background because when we are do that, so I’ll do employee Dot full honour now[ when] we run it from the class we have to manually pass in the instance as an controversy 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 introduced these side-by-side just so that we can compare them here So these two rows now do the exact[ same] thing, but now 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? speciman that we want to run that programme with so we do have to pass in the speciman and that comes progressed in as soul and if I croak[ onward] and Print this out and run it then you can see that it undertakings Just like if we wish to reproduce out the employee one fleck full refer And I wanted to show you that because that’s actually what’s going on in the background when we run employee one speck full refer it gets transformed into this here employee dot full specify and Passes in employee one as soul and that’s why we have self for these methods, so I hope that moves feel to you That’s not extremely important to know when we’re just getting started with working with first-class But we need to understand that[ in] last-minute videos formerly we start inheriting from other classes and things like that So I figured it would be a good thing to go ahead, and substantiate you that now okay? So I think that’s going to make love for this video in this video We learned how to create simple classifies the difference between a class and an instance of that class and we likewise learned how to initialize class attributes and organize procedures 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 heard now 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 experience these tutorials and would like to support them then there are several ways you can do that The easiest channel is to simply like the video and dedicate it a thumbs up and also it’s a huge help to share these videos with Anyone who you[ visualize] got to find them useful and if “youve had” 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]

For more info click here

Related posts

Leave a Comment