Heads up! To view this whole video, sign in with your Treehouse account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Practice Emulating Built-ins!
      
    
You have completed Practice Emulating Built-ins!
Preview
    
      
  View the practice challenge and learn the tasks to complete.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      [MUSIC]
                      0:00
                    
                    
                      Hi there, Pythonistas, Megan here.
                      0:09
                    
                    
                      And in this practice session you'll
be testing your knowledge of
                      0:11
                    
                    
                      emulating built-in methods
inside of given classes.
                      0:15
                    
                    
                      On the screen here you can see I
have two classes one called Dog and
                      0:20
                    
                    
                      the other called Shelter.
                      0:24
                    
                    
                      And what we're going to do is
you're going to add a dunder
                      0:27
                    
                    
                      string method to Dog that
will return the pet's name.
                      0:31
                    
                    
                      You'll also add a dunder equals
method to class Dog that will
                      0:36
                    
                    
                      check if the two dogs names are the same.
                      0:41
                    
                    
                      So given two dogs names,
if they are the same name,
                      0:45
                    
                    
                      it should return that they are equal.
                      0:48
                    
                    
                      And then last, you're going to add
a dunder iter method to class shelter,
                      0:51
                    
                    
                      to iterate through
the animals in the list.
                      0:57
                    
                    
                      So let's take a look at the code
that I've already given you here.
                      1:01
                    
                    
                      For class Dog,
I've given you an init method.
                      1:05
                    
                    
                      And I've set a default value of the name
to be Maddie, which is my puppies name.
                      1:08
                    
                    
                      Feel free to change it or
remove it, completely up to you.
                      1:14
                    
                    
                      It just makes things a little
bit faster sometimes.
                      1:18
                    
                    
                      So then self.name =
name to set that value.
                      1:21
                    
                    
                      Inside of Shelter,
also given you an init method,
                      1:25
                    
                    
                      and I've given you a list of animals.
                      1:29
                    
                    
                      And then I've given you
a function called add_animal,
                      1:32
                    
                    
                      which adds animals to your animals list.
                      1:35
                    
                    
                      And then let's take
a look down here below.
                      1:39
                    
                    
                      I've created three dogs for
you, maddie, jethro, and luna.
                      1:42
                    
                    
                      So, you can see this one I didn't have to
pass in a name, cuz I'm using the default
                      1:47
                    
                    
                      and then I passed in these two names for
the two other dogs.
                      1:51
                    
                    
                      So I've given you three dogs.
                      1:54
                    
                    
                      I have created a shelter, which is
actually one of my favorite shelters.
                      1:56
                    
                    
                      And then I've added those three
animals to the shelter already.
                      2:00
                    
                    
                      And then down here at
the bottom I've created a loop.
                      2:06
                    
                    
                      So for pet in our shelter,
if the pet's name equals maddie,
                      2:09
                    
                    
                      which is the variable that
holds our maddie dog here,
                      2:16
                    
                    
                      then it's going to print out equal.
                      2:22
                    
                    
                      And it's going to print out the pet
itself which will be the pets name coming
                      2:26
                    
                    
                      from your dunder string, and this will be
coming from your dunder equals method.
                      2:31
                    
                    
                      And of course your for loop here
is coming from dunder iter method,
                      2:37
                    
                    
                      I kinda put all three in one.
                      2:41
                    
                    
                      And then of course,
if their name is not equal to maddie,
                      2:43
                    
                    
                      then it's going to print not equal,
and the pet as well.
                      2:47
                    
                    
                      So with everything completed when I
run the file, python pets.py, I should
                      2:51
                    
                    
                      get equal because the first pet in our
list is Maddie, which is equal to itself.
                      2:57
                    
                    
                      So that will give us equal.
                      3:04
                    
                    
                      And then the other two pets are not
named Maddie so I get not equal.
                      3:05
                    
                    
                      Jethro not equal, Luna.
                      3:09
                    
                    
                      Give it a try and see how you do.
                      3:13
                    
                    
                      Best of luck.
                      3:15
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up