Locked lesson.
About this lesson
Finishing up our tutorial on classes.
Exercise files
Download this lesson’s related exercise files.
Creating A Class Part 3.docx61 KB Creating A Class Part 3 - Solution.docx
61 KB
Quick reference
Creating A Class Part 3
Let's add some functionality to our class.
When to use
Create additional functionality for a class by adding method that do specific things.
Instructions
To create additional functionality, just add methods to your class!
You can create as many methods in your class as you like.
To call a method, just slap a dot and the method name onto your instantiation:
employee_1.method_name()
Add the parameter "self" to any method you create inside your class.
You can reference any instance variables from other methods in your class, in any method in your class.
Hints & tips
- Methods add functionality to a class
- Add the parameter "self" to any method you create inside your class
- 00:04 Okay, so we've got our class, we've got our initialization method,
- 00:07 our __init__ method.
- 00:08 And we're passing in these parameters,
- 00:11 we're turning them into instance variables.
- 00:13 Instance variable just means you can use it inside of the class,
- 00:16 in all of the different methods that you create.
- 00:19 In our case, we've only got one method here, but that doesn't really matter.
- 00:22 So right now, we can print out all of these things, and that's kind of fun, but
- 00:26 it's not all that useful.
- 00:27 Let's create another method and give this class a little bit more functionality.
- 00:31 So super easy to do.
- 00:33 And be sure your definition statements line up, because indentation is important,
- 00:37 obviously, in Python, and especially in these classes.
- 00:40 So to create a new method, let's go def, and then let's call this report.
- 00:45 We want to create a little report of our employee, right,
- 00:48 because we've got all this information, let's create a report.
- 00:51 So we create a method, which is basically just a function, and like any function,
- 00:54 you gotta pass in those parameters.
- 00:56 And remember, when we built this one, I said all of our methods are going to start
- 01:00 with self, and that's true here too, so self.
- 01:02 And if we wanted to pass in other stuff, we could do it here.
- 01:05 But we don't really want to pass in anything in this method,
- 01:08 we just want to get information out of it.
- 01:09 So then we got our colon, now what do we want to do?
- 01:12 Well, let's take all this information and sort of format it into a little report.
- 01:16 And we can do that a bunch of different ways.
- 01:17 I'm just going to do a real quick print statement, just to sort of show this.
- 01:21 And so let's go, self.first_name +, and then I'm going to put a little space.
- 01:27 And this is how you add little spaces to things, just add this +.
- 01:30 I think we've probably looked at this before.
- 01:32 And then self.last_name, and then another little space,
- 01:38 and then let's go, self.email.
- 01:41 You notice we have to address these as self, you can't just put the name itself,
- 01:45 because up here, they're self., self., self.
- 01:48 So whenever you use them throughout your class, you have to do self., as well.
- 01:52 So then self.pay, now, you see right away, we get this X here, all right?
- 01:59 There, it disappeared, but nonetheless, we're still going to get an error.
- 02:02 And in fact, if we come down here, to use this to access the report method,
- 02:07 we just type in report.
- 02:08 And then like any method that we want to call, any function we want to call,
- 02:12 we have to pass in the parameters.
- 02:13 Of course, in this case, we don't have any, but
- 02:15 we still have to put these little parentheses.
- 02:16 Nonetheless, if we run this,
- 02:18 we're still going to get an error because, see this self.pay?
- 02:22 These are all strings, but pay is an integer.
- 02:24 And so we're trying to add an integer to a string, and you can't do that.
- 02:28 You can add strings to strings and do it like that,
- 02:31 you can't do it with an integer.
- 02:33 But we want to access this, so
- 02:35 we already learned in the past how to convert an integer into a string.
- 02:38 We just call the str function, and wrap it all in there.
- 02:43 So now that should work.
- 02:44 But from now on, this is a string in this method, so keep that in mind.
- 02:49 So now if we run this, we get John Elder, email address.
- 02:53 We forgot a space, put a space in there, there we go, John Elder, and we get this None.
- 03:00 We could play around with this, and turn it into a return if we like,
- 03:05 get rid of that.
- 03:06 So just like that, we have this nice little John Elder, email, and
- 03:10 hey, and that's the little report that we can do.
- 03:12 And this will work irrespective of what information we put in here.
- 03:17 So we can create employee number 2, and set that equal to Employee of Tim,
- 03:26 Smith, Tim@tim.com.
- 03:30 And poor Tim only makes $40,000.
- 03:34 So now if we come down here and
- 03:36 just change this from employee_1 to employee_2, boom.
- 03:40 Tim Smith, Tim@tim.com, $40,000.
- 03:42 So this is the essence of creating a blueprint.
- 03:45 It doesn't matter what inputs you put into it,
- 03:49 you're going to get out whatever the blueprint has done.
- 03:52 So we put in Tim, we get out Tim, we put in John, we get out John, so very,
- 03:56 very cool.
- 03:57 And I hope that sort of shows you kind of the blueprint-iness
- 04:01 of what classes are and how they work.
- 04:03 And you see this .report, anytime you create a method inside your class,
- 04:07 you just call it just like this.
- 04:09 So if we created a method called fire, like we're going to fire an employee,
- 04:14 then we would come down here and def fire(self).
- 04:21 And then whatever we wanted that method to do, we would just print it out.
- 04:25 And then whenever we wanted to access that, we'd just call the fire method,
- 04:30 right?
- 04:30 Very cool, just let me get back to report, very awesome.
- 04:34 And in this case, we didn't create any other parameters.
- 04:36 But if we did, you would put them right here, separated by commas,
- 04:40 like you always do with other functions, and it's just that easy.
- 04:43 So that's classes, just awesome, right?
- 04:46 So in the next video, we're going to look at built-in class attributes.
Lesson notes are only available for subscribers.