Updating the Descriptor Class
00:00 With the basic structure now in place, you can start building in your own magic. Now remember, your objective is to build a validation method that checks whether the title is a string, and actually you’ll add another check, namely that the title is of a certain minimum length.
00:20
Now, to do that, you will need to build an instance method, call it validate() into the StringValidate class. So line eight if you make some space, and I’ll just paste in the validation instance method because it’s quite a bit of typing.
00:37
So here’s your validate() instance method, which takes self as an input parameter because it is an instance method. And then value, which is of course the value it will need to check on line 10.
00:49
It uses isinstance to check whether value is indeed a string. If that is not the case, and it will raise a TypeError on line 11.
00:59
And it will also check to see if the length of the value is below a certain minimum size. And if that is the case, it’ll then raise a ValueError.
01:10
So on lines 12 and 14, there is the use of .minsize, which is an attribute that hasn’t been defined yet, and you will define that as a class attribute.
01:21
So at the very top of your class, so line three, make some space, indentation, and then minsize equals, and let’s pick 2. So now that there is this validate method, have a think about where you would use it in your code.
01:39
Remember in Song, if you scroll down a little bit, the idea was to check the title in line 24. So when the __init__() method is being run, that’s when I would like the validation to happen.
01:54
So line 24 says self.title equals something else. So this is where self.title is being updated, so the place to do the validation is in the .__set__ dunder method.
02:06
So move back up to line 10 and then delete pass. and just go self.validate(value).
02:18
If this validation fails, then an error will be raised, either a TypeError as per line 14, or a ValueError as per line 16. So if validation passes, the next line of code will be executed.
02:32
And just for clarity, I’d like you to add the following line of code, which prints an f-string, just to show that the set method has been triggered and that it’s updating _.title to value.
02:46
Now this is an important point. The __set__ dunder method is going to update a value in the namespace of the Song class instance.
02:58
So in the namespace of obj, of obj up here. The name of that instance attribute should not clash with any existing class attribute names.
03:11
So title already is a class attribute name. So you’ll use _.title for the instance attribute name.
03:21
How do you do that? Well, there is functionality for that, and that’s called set attribute or setattr().
03:29
The first input parameter is obj, then the name of the attribute, and then the value that that attribute should take. So setattr() is saying in obj, find an attribute that has a name “_title”, and assign a value to that.
03:50
So this is triggered during the __init__() method, okay. So as soon as the Song class is initiated, there will be an attribute in the namespace that is called _title.
04:03
And not only that, that attribute _title will be validated because in line 10 the validation has happened. Now that means that from the Song instance namespace from the namespace of obj, you can now retrieve this _title attribute value.
04:23
And that’s what you’re going to do in the __get__ method in line six and seven. So in get you want to retrieve the value in the Song class instance with the namespace.
04:44
again in obj because that represents the Song instance, and the name is _title.
04:53 Again, just for clarity, it would maybe be nice to add the following print just so you can see what’s happening in the terminal, and then you return the value.
05:07 So that’s the code, please save it. And in the next lesson, you will be using it in the REPL and then we’ll discuss it and see what improvements can be made.
Become a Member to join the conversation.
