1 00:00:00,000 --> 00:00:04,822 You know object-oriented programming is a way to structure 2 00:00:04,822 --> 00:00:08,993 your code into groups of properties and behaviors. 3 00:00:08,993 --> 00:00:11,900 But what does that look like in Python code? 4 00:00:13,080 --> 00:00:18,940 Let's use our car example to build out an object, or class as it's called in Python. 5 00:00:20,790 --> 00:00:24,890 Use the workspaces attached to this video to follow along. 6 00:00:24,890 --> 00:00:31,240 You can also download the file and use an IDE of your choice if you'd rather. 7 00:00:33,070 --> 00:00:36,718 In Python objects are created using the class keyword and 8 00:00:36,718 --> 00:00:39,607 the name of your class with a colon like this. 9 00:00:44,013 --> 00:00:46,820 We're naming our class, Car. 10 00:00:46,820 --> 00:00:50,440 Always remembered to try and name things descriptively when coding. 11 00:00:50,440 --> 00:00:54,320 Classes, functions, variables, etcetera. 12 00:00:54,320 --> 00:00:57,490 Inside of our class, we're just going to add, pass. 13 00:00:59,240 --> 00:01:04,020 So Python knows to keep moving instead of giving us an error. 14 00:01:04,020 --> 00:01:06,990 If we don't have pass inside of our class, 15 00:01:06,990 --> 00:01:13,570 when we run the file, Python gives us a syntax error. 16 00:01:13,570 --> 00:01:16,249 Unexpected end of file while parsing. 17 00:01:20,392 --> 00:01:23,328 You may also see a class written like this, 18 00:01:23,328 --> 00:01:27,760 with a set of parentheses after the name of the class. 19 00:01:27,760 --> 00:01:30,690 Classes can take arguments. 20 00:01:30,690 --> 00:01:35,240 We won't get into this idea called inheritance in this course. 21 00:01:35,240 --> 00:01:39,720 But I wanted to make sure you know that a class can be written with or 22 00:01:39,720 --> 00:01:44,560 without the parentheses, in case you come across it out there in the wild. 23 00:01:44,560 --> 00:01:47,550 Classes are kind of like a factory. 24 00:01:47,550 --> 00:01:51,410 They hold all of the information about an object like a blueprint. 25 00:01:52,690 --> 00:01:56,490 When you use your class to create an object in Python, 26 00:01:56,490 --> 00:01:59,100 it's called instantiation. 27 00:01:59,100 --> 00:02:03,070 And the object itself is called an instance. 28 00:02:03,070 --> 00:02:04,030 Let's create one now. 29 00:02:05,310 --> 00:02:08,950 You create an instance of class car like you would a function. 30 00:02:08,950 --> 00:02:12,690 Use the name of the class followed by parentheses. 31 00:02:12,690 --> 00:02:15,024 You can also save the instance to a variable. 32 00:02:18,196 --> 00:02:21,670 Now I know our class doesn't have a lot to it. 33 00:02:21,670 --> 00:02:24,280 Well, it has nothing to it right now. 34 00:02:24,280 --> 00:02:27,750 But we can still check to see if we made an object. 35 00:02:27,750 --> 00:02:30,120 Let's try printing out our instance. 36 00:02:30,120 --> 00:02:31,715 And let's also check its type. 37 00:02:34,499 --> 00:02:46,384 print(my_car), print(type(my_car). 38 00:02:51,994 --> 00:02:57,020 The first message shows we created an instance of our car class. 39 00:02:57,020 --> 00:03:01,364 It's an object at this location in memory. 40 00:03:01,364 --> 00:03:06,205 The second message tells us our objects type is class car. 41 00:03:06,205 --> 00:03:08,488 Cool. 42 00:03:08,488 --> 00:03:13,830 There's also a way to check if something is an instance of a class. 43 00:03:13,830 --> 00:03:17,480 You can use a built-in function called isinstance, 44 00:03:17,480 --> 00:03:20,580 and pass in the object you want to check. 45 00:03:20,580 --> 00:03:22,600 And the class you want to check it against. 46 00:03:23,610 --> 00:03:26,481 It returns a Boolean value telling us whether or 47 00:03:26,481 --> 00:03:29,582 not the instance is an instance of the given class. 48 00:03:32,460 --> 00:03:38,035 Print(isinstance(my-car and 49 00:03:38,035 --> 00:03:43,612 Car with a capital C for the class. 50 00:03:46,714 --> 00:03:47,600 Run the file. 51 00:03:48,940 --> 00:03:53,160 Our example here is pretty obvious but it might come in handy later on. 52 00:03:54,610 --> 00:03:58,900 I've just thrown a lot of new terms and information at you. 53 00:03:58,900 --> 00:03:59,720 So let me recap. 54 00:04:00,790 --> 00:04:03,630 In Python, you create a blueprint for 55 00:04:03,630 --> 00:04:09,220 your object using the class keyword, the name of your class, and a colon. 56 00:04:09,220 --> 00:04:14,520 When you create an object using your class, it's called instantiation. 57 00:04:14,520 --> 00:04:18,010 The object you've created is called an instance. 58 00:04:19,270 --> 00:04:25,280 You can use print, type, and isinstance to see if you've correctly made an object. 59 00:04:27,000 --> 00:04:33,260 Try creating a few empty classes of your own, then create instances of them. 60 00:04:33,260 --> 00:04:36,925 Check your work using print, type, and isinstance. 61 00:04:37,980 --> 00:04:39,885 Keep up the hard work Pythonistas.