Check out a free preview of the full Intermediate Python course
The "Magic Methods Review" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:
Nina writes a class that returns data about Github repositories, and reviews when to use the magic methods covered in an earlier section.
Transcript from the "Magic Methods Review" Lesson
[00:00:01]
>> Nina: Let's say that we wanted to write a custom class that represented the GitHub repositories that we did in our final exercise in introduction to Python. So our final exercise.
>> Nina: Let me open up that file quickly.
>> Nina: So if we remember in our final exercise, we created a query and then we got back this list of items from the GitHub API.
[00:00:41]
And then we looped through all the results, which is a dictionary. And we got the language, we got the number of stars, we got the name, and we printed this out. How could we refactor this into a custom class?
>> Nina: So the first thing we would wanna do is make a class called GitHubRepo.
[00:01:06]
And we would wanna create a constructor here. And what are the variables that we would take here?
>> [INAUDIBLE]
>> Nina: Not languages, right? The repo has a single language, right? So this is each repository. So it would take a name, a language, and the number of stars that it has.
[00:01:31]
And then we would just assign these values to the instance.
>> Nina: Okay, if I wanted to take this printout here, a string, and make it so it's available on every instance of my GitHubRepo class. What method would I wanna put it in? This nice human readable printout.
>> Speaker 2: String.
[00:02:02]
>> Nina: Right, and not just string, but what?
>> Speaker 2: Dunder string.
>> Nina: Dunder str, right, and that also takes a self. We don't need any of the other variables because we know that they're attached to the instance, and we wanna return a string here, right?
>> Nina: And use self.name, self.language, and self.strs.
[00:02:30]
>> Nina: Okay, so I'm going to run this file again in this interactive mode and now I can make a new GitHubRepo. And again, even though these are required variables, I can provide names here if I want to. So let's say this is,
>> Nina: Vue and the language is JavaScript.
[00:02:59]
And the number of stars is 50,000.
>> Nina: I want to save this into a variable. Why are saying this here when we make a new GitHubRepo?
>> Speaker 2: It's a generator.
>> Nina: Well not a generator, I only implemented str but I didn't implement what other method?
>> Speaker 3: Wrapper.
>> Nina: Wrapper, and when we enter this instance into the REPL without a print or anything like that, it uses the wrapper.
[00:03:35]
Know that I have assigned a variable, vue, I can double check that assumption. If I ask for the wrapper it's that. And keep in mind for those kind of built in types, the zip, the range, if you see something like that it means that it might be a generator.
[00:03:56]
It's not guaranteed, but if you see this for your own custom classes it means that you forgot to implement str or a wrapper. But now if we work at str(vue), oops sorry, I made a mistake here. It's num_stars and I'm just going to copy this into the file so that I can just run print(vue) here.
[00:04:26]
Under the hood print call str because we're using print when we wanna represent to the user and str is the one that is the user friendly one. So I'll run that Python file in the terminal, oops, I have to,
>> Nina: For some reason my VS Code terminal just is not happy.
[00:04:49]
So I'm gonna delete that one and run it again. And there we go, we'll see that vue is a JavaScript repo with 50,000 stars. So this is how we start encapsulating some of the data we've been working with.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops