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

Adding a Minor Improvement

00:00 Just before you add a Book data class to your code in this lesson, I’d like you to make one more small change to the code. And this has to do with row 19 where all it does is it instantiates the product class.

00:16 Now, instantiation of products is really something that should be done by the Factory Method. Now we don’t have a Factory Method yet, but we will do soon and that method will be part of a Creator class, which we also don’t have yet, but we do have a creator function. For now, I would like to move the instantiation of the products to the creator function.

00:44 If you scroll down to the creator function here, _get_serializer(), all that means is that you have to add brackets. So in row 30, add brackets and in row 32 add brackets.

00:58 And for the American viewers, I mean parentheses when I say brackets. Now the _get_serializer() creator function returns instances of either the JSONSerializer class or the XMLSerializer class.

01:14 What that means is that in row 19, you no longer need the instantiation of the serializer_product() because serializer_product() in row 18 is already the instance of the product class.

01:29 In other words, we can delete the line of code

01:34 and just add my_product to row 18.

01:39 So now my_product is still the instance of a product class. my_product gets passed into the object_ to_serialize.use_product method, and then what is returned by row 20 is a string representation of my_product class instance.

01:58 Okay, with this minor improvement out of the way, you will now create a Book data class in the next lesson.

Become a Member to join the conversation.