WEBVTT 1 00:00:00.200 --> 00:00:01.550 Hi welcome back. 2 00:00:01.550 --> 00:00:05.460 In this video I wanted to move that abstract class 3 00:00:05.460 --> 00:00:08.750 into our other code just to give you another example 4 00:00:08.750 --> 00:00:10.860 of where this can be useful. 5 00:00:12.020 --> 00:00:15.610 Going over to our saveable.py remember that 6 00:00:15.610 --> 00:00:20.420 it's got this save method that calls a to.dict method 7 00:00:20.420 --> 00:00:24.210 but the saveable class doesn't have that method defined. 8 00:00:25.240 --> 00:00:30.240 However, it relies on a subclass like admin 9 00:00:30.370 --> 00:00:32.730 to implement the to.dict method. 10 00:00:34.340 --> 00:00:37.470 Relying on the subclass to implement the to.dict method 11 00:00:37.470 --> 00:00:42.470 is okay, but it's even better if you can force the subclass 12 00:00:42.670 --> 00:00:44.500 to implement the method. 13 00:00:44.500 --> 00:00:48.870 And now that you have ABC's and abstract methods, you can. 14 00:00:48.870 --> 00:00:53.470 So lets go ahead and implement this using an ABC. 15 00:00:53.470 --> 00:00:55.820 So from abc_import, ABCMeta, and abstractmethod 16 00:00:58.650 --> 00:01:02.780 and now we can define the to.dict method 17 00:01:02.780 --> 00:01:06.150 using this abstract method, we can say to.dict 18 00:01:08.170 --> 00:01:11.450 and then just pass it which means it doesn't do anything. 19 00:01:11.450 --> 00:01:14.830 Now this is happy because it says, "Okay I can see that 20 00:01:14.830 --> 00:01:16.860 "there is a to.dict method defined." 21 00:01:17.740 --> 00:01:20.230 But it doesn't actually do anything in this class. 22 00:01:20.230 --> 00:01:23.570 It only does things in the to.dict. 23 00:01:23.570 --> 00:01:25.900 The whole purpose of the abstract method is to 24 00:01:25.900 --> 00:01:30.540 force the subclass to implement the method that it needs. 25 00:01:32.650 --> 00:01:36.450 Again, the fact that we have this saveable class now 26 00:01:36.450 --> 00:01:39.930 means that admin is an instance of saveable. 27 00:01:41.100 --> 00:01:43.370 And that can be quite good if you want 28 00:01:43.370 --> 00:01:45.740 then a list of saveable things. 29 00:01:45.740 --> 00:01:47.930 You can then call save on each of them 30 00:01:47.930 --> 00:01:49.940 even if they're not all admin objects. 31 00:01:51.120 --> 00:01:53.270 If we cut the saveable from here, 32 00:01:54.390 --> 00:01:57.160 and we put it into user.py, 33 00:02:03.860 --> 00:02:08.860 now user becomes saveable and admin is also a saveable 34 00:02:09.260 --> 00:02:11.610 because it's a subclass of user. 35 00:02:11.610 --> 00:02:15.280 So again, admin has a to.dict. 36 00:02:15.280 --> 00:02:19.370 So when we say some admin object.save, 37 00:02:19.370 --> 00:02:21.980 it'll call admin's to.dict. 38 00:02:21.980 --> 00:02:24.650 If we have some user object and we call save 39 00:02:25.990 --> 00:02:28.750 it'll call the user's to.dict, which doesn't exist 40 00:02:28.750 --> 00:02:30.100 so we have to implement it. 41 00:02:34.360 --> 00:02:35.580 So just like that. 42 00:02:40.310 --> 00:02:43.740 Now the user has a to.dict, admin has a to.dict which 43 00:02:43.740 --> 00:02:47.611 overrides it and they both can be saved by using 44 00:02:47.611 --> 00:02:50.260 the saveable class. 45 00:02:51.920 --> 00:02:56.000 From now onwards you can refer to saveable as an interface. 46 00:02:56.000 --> 00:03:00.700 By the way, sorry, I forgot to define this metaclass there. 47 00:03:00.700 --> 00:03:01.720 This is important of course, 48 00:03:01.720 --> 00:03:03.690 otherwise it doesn't make much sense. 49 00:03:03.690 --> 00:03:06.240 Python won't be able to understand what's going on. 50 00:03:07.240 --> 00:03:11.740 So saveable here is a super class because it's 51 00:03:11.740 --> 00:03:15.650 an abstract class, you can see it's a class here. 52 00:03:15.650 --> 00:03:17.450 In many other programming languages, 53 00:03:17.450 --> 00:03:19.780 this would be known as an interface 54 00:03:20.880 --> 00:03:23.830 because it defines the functionality that 55 00:03:23.830 --> 00:03:25.340 should be in a subclass. 56 00:03:25.340 --> 00:03:27.460 That's what an interface is. 57 00:03:27.460 --> 00:03:31.180 An interface is not a class that you can instantiate. 58 00:03:31.180 --> 00:03:33.100 It is a thing in programming that 59 00:03:33.100 --> 00:03:36.040 defines what a subclass must do. 60 00:03:36.940 --> 00:03:40.190 So in those terms, and to adhere to that definition, 61 00:03:40.190 --> 00:03:45.190 this is an interface, but because it has this save method 62 00:03:45.210 --> 00:03:49.080 that it is actually implemented, we can't say that this 63 00:03:49.080 --> 00:03:51.600 is an interface in the traditional sense. 64 00:03:52.610 --> 00:03:56.800 However, I quite like these quote unquote Python interfaces. 65 00:03:56.800 --> 00:03:59.290 I like them because you can define them as interfaces. 66 00:03:59.290 --> 00:04:03.310 Just say subclasses must implement the to.dict method. 67 00:04:03.310 --> 00:04:06.410 And you can also add some functionality that 68 00:04:06.410 --> 00:04:10.120 is shared amongst the subclasses, like this save method. 69 00:04:10.120 --> 00:04:13.670 In Java for example, you can define interfaces as something 70 00:04:13.670 --> 00:04:17.310 that is separate from classes, completely different things. 71 00:04:17.310 --> 00:04:20.430 So you can define in Java things like these. 72 00:04:20.430 --> 00:04:22.760 The subclass must implement this method. 73 00:04:22.760 --> 00:04:25.190 But in Java you cannot define or at least you couldn't when 74 00:04:25.190 --> 00:04:28.090 I did Java a long time ago. 75 00:04:28.090 --> 00:04:30.860 You couldn't define the save method. 76 00:04:30.860 --> 00:04:33.470 You couldn't say the subclass will be able to 77 00:04:33.470 --> 00:04:36.170 do this as well and implement it. 78 00:04:37.140 --> 00:04:41.400 So pretty useful in Python that these interfaces 79 00:04:41.400 --> 00:04:43.820 can have both definitions of what the subclass must 80 00:04:43.820 --> 00:04:47.160 be able to do, must implement, and also provide some 81 00:04:47.160 --> 00:04:49.710 functionality that will be shared among subclasses. 82 00:04:51.090 --> 00:04:54.440 And this is what an interface is in Python. 83 00:04:54.440 --> 00:04:55.710 It's as close as you're gonna get. 84 00:04:55.710 --> 00:05:00.570 You don't have defined interfaces like in other languages, 85 00:05:00.570 --> 00:05:05.570 like in Java, but you do have abstract methods and ABC's 86 00:05:05.880 --> 00:05:07.760 which allow you to force subclasses 87 00:05:07.760 --> 00:05:09.530 to implement particular things. 88 00:05:09.530 --> 00:05:11.980 And then of course if we go into app 89 00:05:11.980 --> 00:05:16.330 and you have some users. 90 00:05:21.410 --> 00:05:26.210 You'll have to import user of course. 91 00:05:26.210 --> 00:05:31.210 You can then naturally have a list of users, 92 00:05:32.640 --> 00:05:37.640 and then, save them all like that. 93 00:05:40.310 --> 00:05:44.400 This is perfectly fine because each user is a subclass or 94 00:05:44.400 --> 00:05:47.500 implements the saveable interface. 95 00:05:47.500 --> 00:05:49.420 Because they all implement the interface, 96 00:05:49.420 --> 00:05:52.370 you know that they're all gonna have the save method there. 97 00:05:53.610 --> 00:05:56.580 If you're not sure when you're going to use this, 98 00:05:56.580 --> 00:06:00.690 don't worry the purpose of me teaching you about these 99 00:06:00.690 --> 00:06:03.650 advanced concepts in object-oriented programming is not 100 00:06:03.650 --> 00:06:07.000 so you'll immediately get some ideas on what to do with them 101 00:06:07.980 --> 00:06:11.000 but as you develop more, maybe when you get a job doing 102 00:06:11.000 --> 00:06:14.090 Python development or maybe if you already have a job 103 00:06:14.090 --> 00:06:16.670 and you're working more and more with Python, you will find 104 00:06:16.670 --> 00:06:18.620 yourself a situation where this will 105 00:06:18.620 --> 00:06:20.660 be the perfect thing to use. 106 00:06:20.660 --> 00:06:21.760 And knowing about it is going to 107 00:06:21.760 --> 00:06:24.830 let you design programmes much better. 108 00:06:24.830 --> 00:06:27.750 Of course try it, give it a go, write the code, 109 00:06:27.750 --> 00:06:30.440 download the code that I've made available in this section 110 00:06:30.440 --> 00:06:33.330 and play around with it so that you do internalise it. 111 00:06:33.330 --> 00:06:35.820 Otherwise, a few months on the line you will have forgotten 112 00:06:35.820 --> 00:06:38.370 that this exists and if you do encounter a situation 113 00:06:38.370 --> 00:06:40.520 where you'll need this, you won't remember. 114 00:06:40.520 --> 00:06:43.620 So make sure to play around with it and give it a go 115 00:06:43.620 --> 00:06:44.950 and I'll see you in the very next video. 116 00:06:44.950 --> 00:06:47.280 We're going to talk about something else. 117 00:06:47.280 --> 00:06:48.230 I'll see you there.