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

Enforcing Structure With Abstract Base Classes

00:00 In the previous lesson, you implemented the abstract base class for your products and everything went fine looking at the output on your screen here. But what happens if your products don’t stick to the format or to the structure that is enforced by the abstract base class?

00:20 I’d like you to add a method to the abstract class on the row 82.

00:28 Let’s put in the decorator

00:31 and then define some_method(), which takes self as an input parameter and does nothing else, but just define the method. Now, this suggests that all the classes that inherit from the SerializationProductBlueprint, as you can see in line 73, that those classes all should have a method called some_method() as defined on line 83. Now, you haven’t defined some_method() in the JSONSerializer, which starts on line 87, or in the XMLSerializer, which starts line 101.

01:12 So if you run this code now, it should give you an error warning. So make sure that you save the code and restart your REPL,

01:23 and then import from serializer Song, serialize and Book. That’s fine. Let’s create a book. Again, it’s by Stephen King, and let’s serialize this book to XML.

01:36 And now you can see that you will get an error as described down here: Can't instantiate abstract class _XMLSerializer without an implementation for abstract method some_method. So, because you have defined some_method() in your abstract class, and because XMLSerializer inherits from your abstract class, it has to have some_method() in that it does not, and therefore your code crashes.

02:05 So that is the power of abstract classes. It enforces structure upon the classes that inherit from it.

02:14 So now that you know how the abstract classes enforce the structure, don’t forget to delete the abstract method some_method() because as you saw, it makes your code crash.

02:25 So please delete.

02:29 In the lessons covering abstract base classes for products, you have learned a number of things. Firstly, your song and book objects relied on the start_object and add_property methods that were found in the product classes.

02:46 Secondly, you have learned that whilst you might only have two products right now, which is still a very manageable number, checking that your products follow a certain structure without enforcing that structure when your number of products grows, will become increasingly more difficult.

03:04 You’ve also learned that abstract base classes enforce that structure, and that therefore applying abstract base classes will actually future-proof your code in case your number of products grows, or your number of client objects.

03:21 So now that you have implemented the abstract base class for the Products, in the next lesson, you will implement it for the Creator.

Become a Member to join the conversation.