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

Converting to Creator Class

00:00 In this lesson, you will convert the creator function into a Creator class, and add some functionality to the class just to give it a bit of context why it is important to convert the function into a class.

00:16 Before you look at the code, just a quick note. Earlier in the course you’ve used the string dunder methods, so the __str__ dunder methods for the string representation of your product classes.

00:30 Now shortly you will be using another dunder method called __repr__, so REPR, which also provides a string representation of a class. So what is the difference between the two?

00:42 Well, the string dunder method provides an informal string representation of an object and it’s therefore aimed at the user. Whereas the __repr__ dunder method provides an official string representation of an object, and that is therefore aimed at the program.

01:00 You will be deviating from this intended use of the __repr__, dunder method just to create some extra text output when you’re already using the string, dunder method.

01:10 So this is just for a bit of fun and just to add some lightness to the code.

01:15 So if you scroll down to the creator function here called _get_serializer(). To create a class, as always, type class and call it SerializerCreator.

01:29 And this is a class, so I am using camel case. You will want to instantiate this class. So we need the __init__() method,

01:41 create an attribute for data formats or self.data_format,

01:48 and that is None at the moment. You then want to convert the def get_serializer() to a method as opposed to a function. So press Tab, and now the indentation is such that get_serializer() is now part of the SerializerCreator class, therefore it is a method of that class and therefore the first parameter should be self.

02:14 Then add a line to assign the data format to the attribute self.data_format. So self.data_format, which in the __init__() method was set to None.

02:27 We can now assign the value data_format.

02:31 The rest of the code stays the same. Row 47 still checks the data format is JSON, in which case it would return the instance of the JSON product row 48. If according to row 49, the data format is XML, then row 50 will return the XML serializer instance.

02:51 And if it is neither JSON nor XML, then row 51 and 52 will raise a ValueError.

03:01 To add some functionality, I would ask you to play around with some dunder methods for a little bit. So if you scroll down and add a dunder method. The first one would be the __repr__ dunder method, REPR.

03:18 And if you haven’t come across that dunder method yet, no problem. In a minute you’ll see what that method does. What I would like to return is just a line of text.

03:29 For example, your creator today is SerializerCreator. That is just to show that this line of code was triggered from within the SerializerCreator class as defined in line 41.

03:43 The second dunder method you’ll be playing around with is the __str__, which you are familiar with by now.

03:53 And the intention here is to give the client a bit more information about what’s happening by showing a multi-line string. So it says, dear client, thank you for your purchase of, and then in line 62 Serializer, and then show the attribute self.data_format.

04:13 So that will be printed, and in line 64 we call the __repr__ method. Line 66 just says, Your product is below. And the idea is that then below we show either the JSON or XML, or whichever representation of the book or the song, or whichever object was asked for.

04:34 To make this new Creator class work, you have to make some changes to the serialize() function,

04:42 but firstly, you will need to instantiate a creator. Your creator is now a class. So creator is the instance of the SerializerCreator() class.

04:58 My product will then be the instance of a product class, which is still being triggered by the _get_serializer() function, but this is no longer a function.

05:09 This is a method of the Creator class. Therefore creator._get_serializer(). Now you will get a squiggly line depending on your IDE because _get_serializer() starts with an underscore, which means it’s supposed to be a non-public method.

05:28 It is non-public to the class it was created in, so therefore it is non-public or internal to creator. Yet somehow in row 34, I’m using it outside of the Creator class.

05:42 Therefore, a better thing to do would be to go to creator and in line 46, get rid of the underscore,

05:51 then scroll back up and in line 34 also get rid of the underscore. That doesn’t change anything in terms of functionality. It is just that certain IDEs, depending on their settings, will highlight the use of non-public methods or attributes outside of their class.

06:10 That’s great. The only other change I would like you to make is to add a line of code that prints the creator. So print(creator). And why is that?

06:24 When you print creator, it will print the string representation of the Creator class, which you will remember is what is triggered by the __str__ dunder method in row 59.

06:38 In other words, it’ll print the multiline string on lines 62 to 69. So what you are doing here is providing some extra information to your client. So please save this and go to the REPL and test the code.

06:56 So once you’ve saved the code and restarted the REPL, type the familiar from serializer import Song, serialize, then I use up arrows to create my song, just to give it a different ID for now, 156.

07:13 And then I’ll serialize my song to JSON. And now you see that the output has changed significantly. You have added quite a bit of extra information there using the __str__ dunder method.

07:27 So there’s Dear client, thank you for your purchase of:, the Serializer: JSON, and then your creator today is a SerializerCreator.

07:35 Your product is below, where you will then find the output as before. So the ID, the title and the artist. If you use your up arrows again, and instead of JSON try XML, you’ll get something very similar except that your output now makes it clear that this is indeed XML and you have your XML output at the bottom.

08:00 So that’s it for the creator for now. Later on you’ll revisit the creator, but for now, I’d like you to revisit the products and to bring some extra structure to your products using abstract base classes in the next lesson.

Become a Member to join the conversation.