Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Creating a Class Part 2.docx60.8 KB Creating a Class Part 2 - Solution.docx
59.7 KB
Quick reference
Creating a Class Part 2
Let's continue on with classes and fill out our __init__ method.
When to use
Do this virtually every time you create a class.
Instructions
To create an initialization method:
class Employee:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
To initialize this class, be sure to pass in the required parameters:
employee_1 = Employee("John", "Elder")
To call out something from this method:
print(employee_1.first_name)
Hints & tips
- In your __init__ method, create variables out of the passed-in parameters
- 00:04 So now we've got our class, we've initialized it here, and
- 00:07 we've got some different parameters that we're trying to pass in.
- 00:10 But after that, you can see it doesn't really do anything.
- 00:12 So we need to be able to get these things from these arguments here
- 00:17 into our class so that we can do stuff with them.
- 00:19 So super easy and that's where this self thing comes in.
- 00:22 So we can just go self.first_name = first_name.
- 00:29 So what's going on here is we're taking this parameter, this argument,
- 00:33 whatever we've passed in, and we're slapping it into this and
- 00:36 then assigning it into this variable right here.
- 00:39 So you could, if you wanted to, you could change the name of this to f_name or
- 00:43 anything else, but I don't recommend that you do that.
- 00:45 It's a good idea to keep all of these things as the same name,
- 00:51 it gets confusing otherwise.
- 00:53 It's just easier to read if you do that,
- 00:54 so I recommend, you keep all these things the same name.
- 00:57 So now, we can go self.last_name = last_name and
- 01:03 self., what's next, email = email and
- 01:08 then finally, self.pay = pay.
- 01:12 So if we save this, we can see, now we have these different variables and
- 01:15 we can do stuff with them.
- 01:17 We're not doing anything with them right now, but we can, so
- 01:20 come down here to our instance here of our class that we've instantiated.
- 01:25 We've created an instance we called it employee 1 and
- 01:27 we're getting this little red error, right?
- 01:30 What's going on here?
- 01:31 Well, if we run this, we can see we're getting a big error and
- 01:35 it's a type error takes exactly five arguments.
- 01:37 So we go okay, now we know what's going on.
- 01:39 Remember when we talked about functions way back when and this is a method but
- 01:43 a method is basically just a function inside of a class.
- 01:46 So we call them methods, they're a method of the class.
- 01:49 But anytime we create an instance of a function,
- 01:52 we have to pass in parameters and they have to be the exact same number.
- 01:56 In this case, we have 1,2, 3, 4, we ignore this self.
- 02:02 So we have 4 parameters, and down here, when we instantiated this sucker,
- 02:06 we didn't pass any parameters.
- 02:07 And so that's why it's gone, you've got an error there.
- 02:10 So to do that, super easy, we just create an employee, and
- 02:14 I'll be our first employee, I'll be employee number 1.
- 02:17 So we just pass in John, and then last name Elder, and
- 02:21 then let's say email address is johne4196@gmail.com and
- 02:26 that actually is my email address.
- 02:30 If you ever have questions, send them on over and, then pay let's go $300,000.
- 02:36 Now, you'll notice, this are all wrapped in quotation marks and,
- 02:39 this one is not because obviously, this is a number, this is an integer.
- 02:43 We want to be able to do numbery type things with it into the future so,
- 02:46 we don't want to wrap that in quotation marks.
- 02:48 Also, keep in mind, these things have to be in the same order.
- 02:51 If we put pay, if we put that here, that would be a problem, right?
- 02:57 Our system wouldn't know it would start calling first name,
- 03:00 $300,000 if we did that, so
- 03:02 they have to be in the exact same order as they're listed up here, so no big deal.
- 03:07 We save this and run it.
- 03:09 Now we're not going to get an error, but we're going to get that same message.
- 03:12 It just says, okay, you've created an employee instance.
- 03:15 It's sitting right here at this memory, but it hasn't actually done anything yet.
- 03:19 So how do we do stuff?
- 03:20 Well, right now our class doesn't have a whole lot of stuff that it can do, but
- 03:24 we can reference all of these things.
- 03:27 And this is object oriented stuff, we can do objecty type things.
- 03:31 In this case, the objecty thing is to call a first name or last name.
- 03:36 sort of like with a string, we can make it upper case, well here, we're going to call
- 03:39 first name, that's going to be the method thing that we're going to call.
- 03:43 And to do that, we just go employee_1.first_name.
- 03:47 Save this and run it.
- 03:50 Boom, we get John.
- 03:51 That's so cool, right?
- 03:53 Last_name, Elder, and we can go through all of these.
- 03:57 Pay, boom $300,000.
- 04:01 We could go pay + 200,000.
- 04:05 This is a number we can do numbery things to it and we can just keep plugging away.
- 04:10 We could do first _ name and add on another swapcase.
- 04:18 So we've got dot this, dot that.
- 04:20 Don't forget to put our function parentheses after swapcase,
- 04:23 to save this and run it, boom, John Elder.
- 04:25 So that's sort of an object orientedy thing.
- 04:28 You could just keep putting these dot objects on the end of it.
- 04:30 So that's pretty cool.
- 04:31 Right off the bat, we've got our class.
- 04:34 It does sort of something.
- 04:35 It doesn't do a whole lot right now, but we can put information in and
- 04:38 we can get information out and that's pretty cool.
- 04:41 In the next video, we're going to continue on, we're going to make our class a little
- 04:44 more functionable, add some more stuff to it.
- 04:46 And that'll be in the next video.
Lesson notes are only available for subscribers.