1 00:00:00,000 --> 00:00:03,800 All right. Let's talk about a more advanced subject here called 2 00:00:03,800 --> 00:00:04,800 an interface. 3 00:00:04,800 --> 00:00:08,400 Now we're getting into the real nitty gritty of Python as 4 00:00:08,400 --> 00:00:09,400 a coding language. 5 00:00:09,420 --> 00:00:10,830 Interfaces are a cool thing. 6 00:00:10,840 --> 00:00:13,020 A lot of people actually don't know them. But when you're 7 00:00:13,030 --> 00:00:16,090 working with a larger framework, you're going to be using 8 00:00:16,100 --> 00:00:17,800 interfaces without even knowing about it. 9 00:00:17,840 --> 00:00:19,190 So what is an interface? 10 00:00:19,200 --> 00:00:22,500 An interface is literally just a template for your classes. 11 00:00:22,500 --> 00:00:25,900 It's actually just, it's a blueprint for designing your classes. 12 00:00:26,150 --> 00:00:28,760 So they usually have some sort of base implementation, but 13 00:00:28,770 --> 00:00:31,550 they also have a set of rules for you to implement changes 14 00:00:31,550 --> 00:00:33,299 on a new subclass. 15 00:00:34,100 --> 00:00:37,100 So let's go ahead and sort of recreate the example from our 16 00:00:37,100 --> 00:00:42,200 last video where we have 'class Animal', and we have 'def speak(): 17 00:00:42,900 --> 00:00:50,200 print("Says a thing")', and then we have 'class Cat', which inherits 18 00:00:50,200 --> 00:00:54,400 from 'Animal', and doesn't need to do anything yet. 19 00:00:55,400 --> 00:00:59,300 And if we do, 'henry = Cat()', 20 00:00:59,700 --> 00:01:03,900 'henry.speak()', "Says a thing". 21 00:01:03,900 --> 00:01:06,400 But what if this is our blueprint? 22 00:01:06,459 --> 00:01:11,020 And on every 'Animal' that we subclass to, this is called a 23 00:01:11,030 --> 00:01:12,550 subclass, and this is the base class. 24 00:01:13,100 --> 00:01:16,700 What if we said on every subclass we needed to change this? 25 00:01:16,700 --> 00:01:22,400 Let's go ahead and force this with a rule by raising an exception, 26 00:01:22,400 --> 00:01:24,700 which is the thing we'll talk about in a few lessons from 27 00:01:24,700 --> 00:01:28,400 now I believe, we're going to raise an exception called 28 00:01:28,400 --> 00:01:31,200 'NotImplementedError', 29 00:01:31,200 --> 00:01:35,700 And we can give this an error that says, "Not implemented 30 00:01:35,700 --> 00:01:39,300 please add an animal sound." 31 00:01:40,600 --> 00:01:41,800 Okay, let's run that. 32 00:01:41,800 --> 00:01:46,700 Let's rerun this cell, rerun this cell, and we get the, 'NotImplementedError', 33 00:01:47,100 --> 00:01:51,100 and that's because 'speak' is going to raise an error for us 34 00:01:51,100 --> 00:01:52,800 when we run it from 'Cat'. 35 00:01:52,800 --> 00:01:56,200 Now we're likely never going to run it from 'Animal' since that's a base class, 36 00:01:56,200 --> 00:01:59,500 but in a subclass, we need this to be overwritten. 37 00:01:59,500 --> 00:02:02,000 And what this basically says is, 'That's cool. 38 00:02:02,000 --> 00:02:05,100 Yeah, there's a 'speak' class, but Hey, if you try to use it, 39 00:02:05,160 --> 00:02:06,510 it's not going to work. 40 00:02:06,510 --> 00:02:10,199 You need to overwrite this on every single subclass". 41 00:02:10,280 --> 00:02:12,259 So that's what we're going to do. 42 00:02:12,900 --> 00:02:17,200 This is now our template saying, "Hey, this needs to be implemented". 43 00:02:17,200 --> 00:02:18,900 So let's implement it. 44 00:02:18,900 --> 00:02:20,900 [no audio] 45 00:02:20,900 --> 00:02:26,500 And this is just going to say "Meow". We'll rerun these cells, 46 00:02:26,900 --> 00:02:28,900 and 'henry' now says, "Meow". 47 00:02:29,500 --> 00:02:33,200 But let's take a look at an example with a 'Dog' class. 48 00:02:33,200 --> 00:02:35,300 We've got a 'Dog', it inherits from 'Animal', 49 00:02:35,300 --> 00:02:37,300 and it's not going to do anything either. 50 00:02:38,100 --> 00:02:43,300 We have a new 'doggo', 'doggo.speak()'. 51 00:02:43,300 --> 00:02:46,400 We're going to see the same implementation raised error 52 00:02:46,400 --> 00:02:48,700 [no audio] 53 00:02:48,700 --> 00:02:51,800 where it says 'Not implemented', and 'Not implemented 54 00:02:51,800 --> 00:02:53,200 please add an animal sound". 55 00:02:54,300 --> 00:02:57,900 And that's again, because 'Dog' is a subclass of 'Animal', and 56 00:02:57,900 --> 00:03:01,400 every subclass, we're saying needs to overwrite this. 57 00:03:01,400 --> 00:03:07,400 So 'def speak()', always take 'self' because it is a method on a class. 58 00:03:08,300 --> 00:03:14,900 'print("Woof woof"). "Woof woof". Rerun those cells and it works for us. 59 00:03:14,900 --> 00:03:17,450 Now let's talk about this in here. 60 00:03:17,460 --> 00:03:18,950 This is a little bit different. 61 00:03:20,200 --> 00:03:23,600 We haven't seen the keyword 'raise' yet, but we will be seeing 62 00:03:23,600 --> 00:03:24,600 that eventually. 63 00:03:24,600 --> 00:03:28,900 And all that does is bubbles up an exception, or an error, 64 00:03:28,900 --> 00:03:30,300 or a specific type of error. 65 00:03:30,300 --> 00:03:33,900 This one comes built-in with Python, called the 'NotImplementedError', 66 00:03:33,900 --> 00:03:38,600 and it looks a lot like a function, although it's not lowercase. 67 00:03:39,400 --> 00:03:43,700 It's camel case, a lot like what our classes have been looking like lately. 68 00:03:43,780 --> 00:03:48,340 So this is actually raising a class, and its parameter, its 69 00:03:48,340 --> 00:03:50,400 first parameter, is an error message. 70 00:03:50,400 --> 00:03:53,800 And in a future lesson, we are actually going to be able 71 00:03:53,800 --> 00:03:55,900 to raise an exception like this. 72 00:03:55,900 --> 00:03:58,400 This is called an exception. So we can raise an exception 73 00:03:59,400 --> 00:04:02,900 and we can catch it so that our program doesn't just totally 74 00:04:03,000 --> 00:04:09,890 die. So, for instance, if I comment that out, save, save, 75 00:04:09,900 --> 00:04:14,280 and we get this 'NotImplementedError' on our 'Dog'. We can 76 00:04:14,290 --> 00:04:18,600 actually catch this error, and work with it instead of having it 77 00:04:18,600 --> 00:04:19,899 error out on us like this, 78 00:04:19,920 --> 00:04:23,279 because moving forward, if we have a program that errors 79 00:04:23,279 --> 00:04:25,700 out, chances are it's going to just totally die. 80 00:04:25,720 --> 00:04:28,420 It's not going to know how to handle the error, and it's 81 00:04:28,430 --> 00:04:31,720 just going to totally quit your program, which is terrible. 82 00:04:31,730 --> 00:04:33,100 You never want that. 83 00:04:33,110 --> 00:04:36,670 And so by raising an exception, we can then catch that exception 84 00:04:36,670 --> 00:04:38,500 and work with it gracefully. 85 00:04:38,560 --> 00:04:42,190 And we'll talk about raising exceptions and catching them 86 00:04:42,200 --> 00:04:43,600 a little bit later in depth. 87 00:04:43,680 --> 00:04:47,940 But what I want to talk about here was, this in particular 88 00:04:49,200 --> 00:04:52,300 is going to force us to always implement the 'speak' method 89 00:04:52,300 --> 00:04:54,100 on any subclass that we use. 90 00:04:54,140 --> 00:04:58,310 So the idea here is to not actually use the 'Animal' class, 91 00:04:58,320 --> 00:05:03,570 except to inherit from other classes, or by other classes 92 00:05:03,580 --> 00:05:06,180 rather. So we would never really use this on its own. 93 00:05:06,180 --> 00:05:08,200 It would really only show up in here. 94 00:05:08,900 --> 00:05:11,500 And this is the rule saying that, "Hey, if anytime you're going to 95 00:05:11,520 --> 00:05:14,840 make that animal speak, you actually have to implement 96 00:05:14,840 --> 00:05:17,500 something on your own. That's our blueprint. That's our guide. 97 00:05:17,500 --> 00:05:21,600 That's a perfect template for creating other classes, 98 00:05:21,600 --> 00:05:22,900 subclasses we call them. 99 00:05:22,900 --> 00:05:28,700 And that, in a nutshell, is a class interface in Python.