1 00:00:00,000 --> 00:00:09,166 [MUSIC] 2 00:00:09,166 --> 00:00:11,307 Hi there, Pythonistas, Megan here. 3 00:00:11,307 --> 00:00:15,751 And in this practice session you'll be testing your knowledge of 4 00:00:15,751 --> 00:00:20,270 emulating built-in methods inside of given classes. 5 00:00:20,270 --> 00:00:24,590 On the screen here you can see I have two classes one called Dog and 6 00:00:24,590 --> 00:00:27,030 the other called Shelter. 7 00:00:27,030 --> 00:00:31,715 And what we're going to do is you're going to add a dunder 8 00:00:31,715 --> 00:00:36,216 string method to Dog that will return the pet's name. 9 00:00:36,216 --> 00:00:41,091 You'll also add a dunder equals method to class Dog that will 10 00:00:41,091 --> 00:00:45,250 check if the two dogs names are the same. 11 00:00:45,250 --> 00:00:48,548 So given two dogs names, if they are the same name, 12 00:00:48,548 --> 00:00:51,940 it should return that they are equal. 13 00:00:51,940 --> 00:00:57,690 And then last, you're going to add a dunder iter method to class shelter, 14 00:00:57,690 --> 00:01:01,490 to iterate through the animals in the list. 15 00:01:01,490 --> 00:01:05,436 So let's take a look at the code that I've already given you here. 16 00:01:05,436 --> 00:01:08,900 For class Dog, I've given you an init method. 17 00:01:08,900 --> 00:01:14,252 And I've set a default value of the name to be Maddie, which is my puppies name. 18 00:01:14,252 --> 00:01:18,686 Feel free to change it or remove it, completely up to you. 19 00:01:18,686 --> 00:01:21,090 It just makes things a little bit faster sometimes. 20 00:01:21,090 --> 00:01:25,470 So then self.name = name to set that value. 21 00:01:25,470 --> 00:01:29,280 Inside of Shelter, also given you an init method, 22 00:01:29,280 --> 00:01:32,125 and I've given you a list of animals. 23 00:01:32,125 --> 00:01:35,709 And then I've given you a function called add_animal, 24 00:01:35,709 --> 00:01:38,330 which adds animals to your animals list. 25 00:01:39,810 --> 00:01:42,430 And then let's take a look down here below. 26 00:01:42,430 --> 00:01:45,990 I've created three dogs for you, maddie, jethro, and luna. 27 00:01:47,650 --> 00:01:51,752 So, you can see this one I didn't have to pass in a name, cuz I'm using the default 28 00:01:51,752 --> 00:01:54,680 and then I passed in these two names for the two other dogs. 29 00:01:54,680 --> 00:01:56,860 So I've given you three dogs. 30 00:01:56,860 --> 00:02:00,860 I have created a shelter, which is actually one of my favorite shelters. 31 00:02:00,860 --> 00:02:04,770 And then I've added those three animals to the shelter already. 32 00:02:06,080 --> 00:02:09,510 And then down here at the bottom I've created a loop. 33 00:02:09,510 --> 00:02:16,195 So for pet in our shelter, if the pet's name equals maddie, 34 00:02:16,195 --> 00:02:22,094 which is the variable that holds our maddie dog here, 35 00:02:22,094 --> 00:02:26,175 then it's going to print out equal. 36 00:02:26,175 --> 00:02:31,487 And it's going to print out the pet itself which will be the pets name coming 37 00:02:31,487 --> 00:02:37,328 from your dunder string, and this will be coming from your dunder equals method. 38 00:02:37,328 --> 00:02:41,375 And of course your for loop here is coming from dunder iter method, 39 00:02:41,375 --> 00:02:43,121 I kinda put all three in one. 40 00:02:43,121 --> 00:02:47,013 And then of course, if their name is not equal to maddie, 41 00:02:47,013 --> 00:02:51,420 then it's going to print not equal, and the pet as well. 42 00:02:51,420 --> 00:02:57,472 So with everything completed when I run the file, python pets.py, I should 43 00:02:57,472 --> 00:03:04,028 get equal because the first pet in our list is Maddie, which is equal to itself. 44 00:03:04,028 --> 00:03:05,707 So that will give us equal. 45 00:03:05,707 --> 00:03:09,171 And then the other two pets are not named Maddie so I get not equal. 46 00:03:09,171 --> 00:03:13,263 Jethro not equal, Luna. 47 00:03:13,263 --> 00:03:15,185 Give it a try and see how you do. 48 00:03:15,185 --> 00:03:15,980 Best of luck.