Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Introducing Abstract Base Classes

00:00 In this lesson, you will be introducing abstract base classes, but before you do, I invite you to have a look at your products. So on line 77, you have the JSONSerializer class and on line 91, there’s the XMLSerializer class.

00:17 And just by looking at them, you can see that they follow the same structure. They both have an .__init__() method. So on lines 78 and 92, they both have a start_object() method on lines 81 and 95.

00:31 They both have an add_property() method on lines 84 and 98. And if I scroll down a bit, you can see that both also have a .__str__() method on lines 87 and 102.

00:47 Now the fact that they have the same structure is important because your Song and Book classes depend on it. So if I move all the way up to the top of the code, you will see on line 11 the use_product() method for the Song class.

01:05 Now if I scroll down a little bit, you see the same use_product() method on line 25 for the Book class. And these use_product() methods, they use the start_object() and add_property() methods of the product classes.

01:22 As you can see in lines 12 to 14, and lines 26 to 29. So your products need to be structured in that way for the code to work. So, therefore, it would probably be a good idea to enforce that structure.

01:38 At the moment, there are only two products, the XMLSerializer and the JSONSerializer. So that is probably still quite manageable to check that they have the same structure.

01:49 But if your number of products increases, that might become trickier to check and it might be a better idea to actually enforce that structure. And that is where abstract base classes come in.

02:03 So what are abstract base classes? Well, they are blueprints for other classes and they define the structure that other classes must follow. So in your example, it will define the structure of the product classes as in there needs to be start_object() method, and there needs to be an add_property() method, and there needs to be a .__str__() method.

02:27 And we also have the .__init__() method. And in doing so, abstract base classes enhance code organization and code reusability, and also consistency.

02:41 So now that you know what abstract base classes are, you’re going to look at some code to implement them.

02:47 So at the top of your code in the import section, type from abc, abc in lowercase, import ABC, in capitals comma abstractmethod.

03:03 And if you then scroll down to the Products section, you’ll be writing an abstract base class at the start of that Products section.

03:11 So row 73, if you make some space and then create a class as normal by typing class, and then you can give this class whatever name you want.

03:21 I prefer to give it a kind of a meaningful name. So I’m going to call it SerializationProductBlueprint,

03:29 and I use camel case. And this class, to be an abstract product class, it needs to inherit from ABC, which is short for abstract base class, which you imported at the top of the code.

03:44 Now in this blueprint, you are going to define the methods that all the classes that will inherit from this blueprint will need. So if you look at the JSONSerializer, it has an .__init__() method on line 78, a start_object() method on line 81, an add_property() method on line 84, and then a .__str__() method on line 87.

04:09 Now the dunder methods .__init__() and .__str__() you don’t need to define in your base class, but the other two methods, start_object() and add_property(), you do. So if you want to just copy

04:25 the start_object() method takes .self as an input parameter, and the method itself here in your base class doesn’t need to do anything.

04:34 The point is that you define the method, but you don’t necessarily have it do anything. So all you need in here is pass.

04:44 The same thing for the add_property() method.

04:48 That also takes self as an input parameter and again, all you need is pass. So now you have defined your two methods, start_object() and add_property(), but you also need to add a decorator to that to point out that these are indeed abstract methods.

05:09 So above the method, type @ as you do for every decorator, and then abstractmethod.

05:19 And the same thing for the add_property() method, the decorator @abstractmethod, and that’s it. There you have defined your abstract class, your ABC, your abstract base class, and you have named it SerializationProductBlueprint.

05:38 The next step in this process is to make sure that the classes that will need to stick to this structure, if you like, inherits from this abstract base class.

05:48 Of course, I’m talking about the JSONSerializer and XMLSerializer product classes. So they need to inherit from the SerializationProductBlueprint class.

06:00 So the way to do that is I would suggest you copy this long name and then go to the JSONSerializer class on row 83, open brackets and paste, and then scroll further down to the XMLSerializer product class on row 97, open brackets and paste SerializationProductBlueprint in the brackets.

06:25 So the next thing you do is to quickly check that this all works fine. So if you go to your REPL. So once you have saved the code and restarted the REPL, type from serializer import Song, Book, and serialize.

06:42 And create Song, so my_song, using the same song as before. Then I’m also going to create a Book that also needs an id.

06:54 It needs a title, it needs an author,

07:02 also needs a publisher.

07:07 So now you can play around with the code a little bit. You will serialize, let’s try my_song first

07:15 and then let’s try XML format.

07:20 And there you have it. We have the expected output. It’s telling us it’s XML, it’s telling us it’s a song with id="54", and that is “A Forest” by The Cure.

07:32 I’d like to try a book now. So let’s see if I can serialize It for Stephen King. So my_book and use JSON format.

07:43 Indeed I can, the output is now telling me this is indeed JSON. And below here we have the string representation of the book. It’s by Stephen King with id 777.

07:57 Enforcing that structure hasn’t changed anything because your products were already sticking to that structure. So everything still works as planned. But in the next lesson, you’ll see what happens when you deviate from the structure that has been enforced by abstract base classes.

Become a Member to join the conversation.