Locked lesson.
About this lesson
How to create a simple class.
Exercise files
Download this lesson’s related exercise files.
Creating a Class Part 1.docx60.6 KB Creating a Class Part 1 - Solution.docx
59.3 KB
Quick reference
Creating a Class Part 1
Let's build our first class!
When to use
Use these steps to create a class.
Instructions
To create a class, type the word "class" and then name the class (with an Uppercase name).
class Employee:
def __init__(self, first_name, last_name):
To instantiate that class (fire it up), create an instance of it.
employee_1 = Employee()
Hints & tips
- class Employee()
- employee_1 = Employee()
- 00:04 Okay, in this video we're going to build our first class.
- 00:06 And this is very exciting, very fun stuff, I love classes.
- 00:09 They're tricky for a lot of people to figure out how to use, but
- 00:12 once you sort of understand what's going on hopefully it'll click in your head and
- 00:15 you'll go, okay, and you'll see just easy this is.
- 00:17 So to create a class it's very easy,
- 00:19 we just type in the word class, lowercase, and give it an uppercase name.
- 00:24 And like most things, when we do blocks of code we put a little colon next to it, and
- 00:28 now everything that's indented after this is going to be our class.
- 00:32 So classes are sort of two part structures.
- 00:35 You have the class itself that we're going to write here in a minute.
- 00:38 And then to use a class you have to instantiate that class,
- 00:42 you have to create an instance of that class, and we'll see how to do that.
- 00:45 We'll actually let's do it right now.
- 00:47 I'm just going to type in pass because we learned about pass a while back,
- 00:50 we just want to skip anything, if we just we leave it blank we'll get an error.
- 00:52 But to create an instance of a class you just create a variable name,
- 00:57 and I'm going to call this employee_1.
- 01:00 And we set it equal to create an instance of the class,
- 01:04 the employee class, spell it right.
- 01:07 And if we save this and run it of course nothing happens because we haven't
- 01:10 told it to do anything, we've just created an instance.
- 01:13 But now we can do things with that instance,
- 01:16 we can print out, just like we've done so many times in the past.
- 01:20 And you can see nothing really happens here but we get this weird information,
- 01:24 and we've sort of seen this before.
- 01:26 This is a location in memory where our instance is sitting.
- 01:30 And since there's nothing in this class yet
- 01:32 it doesn't really do anything, there's not a whole lot to see here.
- 01:35 But I did want to show you that we have created an instance, it is sitting in
- 01:39 memory at this location, which you don't really care about but it's kind of neat.
- 01:43 But if you run this again, kind of look at this real quick, it ends in 758.
- 01:47 If we run it again, well it still ends in 758 but the rest of it changed.
- 01:51 So every time you run your programming, create a new instance,
- 01:54 it loads it into a new location in memory.
- 01:57 So just sort of something to keep in the back of your head.
- 01:59 So right now we have this class, it's called employee.
- 02:03 To create an instance of the class we just do it like this, but
- 02:06 this doesn't do a whole lot.
- 02:07 Well the first thing you want to do usually when you create a class
- 02:12 is to create an initialization method.
- 02:14 And I keep saying the word method, and we'll talk about this in a bit, but
- 02:18 a method is a function.
- 02:19 We know all about functions,
- 02:21 it's just a method is a function that works inside of a class.
- 02:24 So instead of calling it a function we call it a method because it works in
- 02:27 a class.
- 02:28 So they're pretty much the same thing.
- 02:29 So to initialize our class, we want to create an initialization method.
- 02:33 So like we create any function, we start with def.
- 02:36 And we do this, two underscores, and then I-N-I-T, init, stands for
- 02:41 initialize, right, and then two more.
- 02:43 And like any function, you have to pass in some arguments.
- 02:47 So all of your methods inside of a class are going to pass in the argument of self.
- 02:52 And this allows us to do things inside of the class by referencing themselves.
- 02:57 You don't really need to know what that means just right now, for
- 03:00 now just know we're always going to stick self in there.
- 03:02 And then after this we're going to put any other parameters we want to pass.
- 03:06 Remember when we did functions earlier, if we want to pass things in,
- 03:09 like the first name or the last name, we do it like this.
- 03:12 So think about an employee, if you wanted a blueprint for an employee,
- 03:16 what would you have?
- 03:17 Well you'd probably have their first name, their last name, maybe their email
- 03:20 address, what their salary is, maybe their home address, maybe their phone number.
- 03:24 So you would pass all of those things in that you want.
- 03:28 So let's just do first_name, let's go last_name,
- 03:33 let's go email, and let's go pay just for fun, right.
- 03:39 So stick with colon because it's like any other function method,
- 03:43 put a colon on there.
- 03:44 Now we need to set each of these out and create a variable for
- 03:49 each of these that we can then use inside of here to do different things.
- 03:52 So that's all for this video, in the next video we'll continue on from here.
Lesson notes are only available for subscribers.