"Ruby Foundations" was retired on September 23, 2015. You are now viewing the recommended replacement.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Basic Object-Oriented Python!
You have completed Basic Object-Oriented Python!
Preview
In this step, learn how to tackle iterations using your created objects.
class Candy:
def __init__(self, name, color):
self.name = name
self.color = color
def __str__(self):
return f'{self.color} {self.name}'
class CandyStore:
def __init__(self):
self.candies = []
def __iter__(self):
yield from self.candies
def add_candy(self, candy):
self.candies.append(candy)
nerds = Candy('Nerds', 'Multi')
chocolate = Candy("Hersey's Bar", 'Brown')
my_store = CandyStore()
my_store.add_candy(nerds)
my_store.add_candy(chocolate)
for candy in my_store:
print(candy)
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
In this video,
we're going to tackle dunder iter.
0:00
This dunder method helps Python know how
to handle iterating through an instance,
0:04
like you would with while or for loops.
0:09
Right now, if we tried to
iterate through our instance,
0:12
we would get something like this.
0:15
Car object is not iterable.
0:38
Right now, Python has no clue what you
want it to do here, so let's help it out.
0:42
Now, it doesn't make a lot of sense
to add this method to our car class.
0:48
What would make more sense is
a group of cars at a car dealership.
0:53
Let's create a new class
called dealership.
0:59
Create our init method.
1:11
And give it a list of cars.
1:17
Now we have something to iterate through.
1:38
Let's add our dunder iter method.
1:41
Inside of our method we'll
add yield from self.cars.
1:54
Yield from is a keyword that tells Python
to grab each item in the iterable
2:01
we're giving it here the cars list and
return it.
2:07
Let's see it in action.
2:13
First, create an instance and then loop
through our cars with the for loop.
2:15
Run it, and the console shows our cars.
2:47
Now, how can we combine our two classes?
2:56
We've got one for cars,
and one for a dealership.
3:00
Seems like a match to me.
3:05
Let's remove our cars in the list here.
3:07
So we're back at an empty list.
3:09
Then we can add a method that lets
us add cars to our dealership.
3:15
We'll call this method add_cars.
3:20
It'll take self and a car for arguments.
3:30
And inside the method we'll
append the car to our list.
3:36
Perfect!
3:46
Now we can use this method to
add some cars to our dealership.
3:48
Let's create a few car instances and
add them
3:52
Copy and
paste to make it a little bit faster.
4:35
Now when we run the file,
4:45
we see our new cars in the console.
4:48
Now, if you didn't notice we've got
two dunder methods working together.
4:55
If we comment out the dunder
string method on our car class,
5:01
And run the file, you can
5:13
see we're back at having these
object messages in the console.
5:18
This is because Python is
going into our dealership,
5:23
And grabbing our list of cars.
5:30
Then when each car instance is printed
out, it activates my trap cards.
5:35
No, but it does activate the dunder
string method of our car class
5:43
Nice work, take a few minutes
to try this out on your own.
5:52
You can create two classes like a book and
bookcase, or
5:57
candy and
candy store to practice dunder iter.
6:01
Check the teachers notes
to see what I came up with.
6:05
Keep up the hard work.
6:09
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