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

Discussing the Descriptor Ouput

00:00 So in your REPL, from validator import Song,

00:06 and I’d like you to create an instance of a Song that has no errors. So s1 = (Song, and then I’m still gonna use "A Forest", "The Cure").

00:15 Of course you can use whatever you prefer, as long as your inputs are strings and they are longer than two characters.

00:25 So I hit Enter and then this line appears, .__set(): Updating '_title' to 'A Forest'. Okay, updating _title to A Forest. So what is happening here?

00:34 Well, exactly what should be happening. Let me open up the code again. So in Song, what happens when you created your S1 is you triggered the __init__ method.

00:44 This __init__ method then executed this line of code, which triggered the `__set__ dunder method up here in the descriptor class and __set__ does a validation.

00:56 The validation has passed because the title was indeed a string and it was longer than two characters. And then it prints this statement.

01:06 So that is what you’re seeing on screen. So, so far so good. Now the next thing that happened, and you haven’t seen that on screen, but is that in the namespace of the object, so of s1 of the Song instance, there now should be an attribute called _title that should have the value "A Forest".

01:30 So let’s have a look at that. The way to look at the namespace is to use vars().

01:36 So vars(s1) indeed now has _title and it holds "A Forest". So now, given that _title is in s1’s namespace, I can actually retrieve it.

01:50 So I could ask s1._title. That is indeed A Forest. So _title is now available in s1’s namespace. That means that it’s also available to the __get__ method, which has access to s1’s namespace through obj.

02:11 Let’s have a look at the code again, line seven.

02:15 That’s exactly what’s happening there; it’s saying get attribute in obj, which is s1’s namespace, and get the attribute with the name _title.

02:27 So now that _title exists in the namespace, we can have __get__ retrieve it. Now, when is __get__ being triggered?

02:35 Well, __get__ is being triggered when down here in the Song class, self.title is being used via the dot notation but not updated.

02:47 So that is what happens in line 32. Line 32 clearly belongs to the __str__ method. So if we use the __str__ method, this should work just fine.

02:57 So in the REPL, type str(s1),

03:02 and then indeed it’s telling us that the __get__ method has been triggered, it’s accessing _title, and that gives us "A Forest", and then we get the output that we wanted.

03:15 The next thing to check is to see if the validation checks work. So if you would create a song that breaks the rules, a title that is not a string, so say 1 and the band is still the same, The Cure,

03:32 you will then have a traceback and at the bottom it will indeed raise a TypeError. It says 1 should be a string. So that validation seems to work.

03:40 And if the validation doesn’t work, it stops the code as that’s what we wanted.

03:46 And then instead of an integer, let’s put in a string, but one that’s only of length 1, for example, that should fail too, because we need 2. And indeed we have a traceback.

03:57 And then a ValueError saying A, which is what I entered as a title, should be of length two or longer.

04:06 The final thing for this lesson is to come up with some improvements for this code. So back up here in the StringValidator class, something I don’t like is the hard coding of the _title attribute name.

04:22 I would like that to be dynamic. And also that would mean I could use this to check the artist’s name as well, because that could be a string or should be a string.

04:30 The other thing to improve is that this minimum size here is hard coded. I also don’t like that, because for example, we could require the title to be two characters long at least, but the group name to be at least five characters long.

04:49 In the next lesson, you will build in this new flexibility.

Become a Member to join the conversation.