Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Bashir Orfani](https://secure.gravatar.com/avatar/a5e0fc4e4b636b8d38eb68a738efbdb8?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Bashir Orfani
13,170 PointsLet's make all Board instances
Let's make all Board instances iterable so we can loop through their cells attribute. Inside the Board class, define an iter method that yields the cells. If you need help, refer back to the "Emulating Builtins" video.
class Board:
def __init__(self, width, height):
self.width = width
self.height = height
self.cells = []
for y in range(self.height):
for x in range(self.width):
self.cells.append((x, y))
class TicTacToe(Board):
def __init__(self, width=3, height=3): # the defaults go here instead
super().__init__(width, height)
3 Answers
![Jeff Muday](https://uploads.teamtreehouse.com/production/profile-photos/5900142/micro_jeff_face.jpg)
Jeff Muday
Treehouse Moderator 28,718 PointsWhich part of this are you having trouble with?
Part 1 is a question about subclassing the Board Object and calling its super().__init__()
method
Part 2 is a question about implementing an __iter__()
method on the new class.
![Kevin LG](https://secure.gravatar.com/avatar/a301f3d8d9f45126ac11d2967be82a37?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Kevin LG
5,641 PointsYou can also streamline your code by skipping the for loop, and using yield from self.cells.
class Board:
def __init__(self, width, height):
self.width = width
self.height = height
self.cells = []
for y in range(self.height):
for x in range(self.width):
self.cells.append((x, y))
def __iter__(self):
yield from self.cells #<------the for loop obviously works,
#but by using yield from you save a line of code to be typed.
class TicTacToe(Board):
def __init__(self):
super().__init__(width = 3, height = 3)
![Benyamin Kohanchi](https://secure.gravatar.com/avatar/0fd723fb6e875076cd2e09761b4a12d1?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Benyamin Kohanchi
Courses Plus Student 2,342 PointsHey Bashir, this is how you do the second part of the challenge.
Kenneth is asking you to create a method that iterates (using __ iter __) over self.cells in the Class Board. This is what the entire code should look like:
class Board:
def __init__(self, width, height):
self.width = width
self.height = height
self.cells = [] # <---- This is what we are trying to iterate over in Challenge 2
for y in range(self.height):
for x in range(self.width):
self.cells.append((x, y))
def __iter__(self): #<----- Second challenge asked us to define __iter__
for cell in self.cells:
yield cell
class TicTacToe(Board): # <---- First challenge asked to make this class
def __init__(self):
super().__init__(width=3, height=3)
Basically, we defined __ iter __ and made it loop over cells....
def __iter__(self):
for cell in self.cells:
...and then we yielded (or returned what we wanted --> without <--- ending the program) each iterable item in the list of self.cells.
yield cell
If you have any questions, comment them down below and ill try to answer them as soon as I can. Good luck!