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

Avoiding More Hard Coding: Input Parameters

00:00 The final change to make is to avoid the hardcoding of minsize, which is a class variable here. And the way to do that is to make that an input parameter into the descriptor.

00:13 And how do you do that? Well, you do that by creating an __init__() method in the StringValidate class.

00:20 So between set and validate, I’m going to create an __init__() method.

00:27 First parameter is, as always, self, and then minsize is going to be an input parameter. And all this __init__() method will do is to define self.minsize,

00:40 and it will assign the minsize value to this attribute. And what that means is that now, if you scroll all the way to the top, you no longer need this class attribute; you can delete that.

00:54 Rest of the code should work just fine. For example, line 23, self.minsize, which was available through a class attribute before, is now available through an instance attribute self.minsize.

01:09 Of course, that means that in the Song class where we instantiate StringValidator, that now needs to have an input parameter and that will be minsize.

01:19 So for title, let’s make that 2. And then what I would like to show you as well is you have made this code so flexible that you could apply it just as well to artist.

01:31 So artist would then need to become a class attribute. You then assign the StringValidator instance to it, and you can use a different minsize.

01:43 Let’s use 3, for example. So what that means is that now artist is also a class attribute to which the instance of a descriptor class is assigned, and therefore in line 34, where self.artist is being used and it’s actually being updated because it’s given the value artist, that will trigger set in the descriptor class and then the whole process starts again.

02:10 In other words, artist will also be validated.

02:14 So those were the final changes. In the next lesson, you’ll use the REPL to test your code, so please save your code, and then reopen your REPL.

Become a Member to join the conversation.