擅长:python、mysql、java
<p>因为您还没有在类上实现<code>__iter__</code>方法。尽管你不需要一个来完成你想要完成的事情:</p>
<pre><code>class Room(object):
def __init__(self,width,height):
self.room = [[False for i in range(width)] for j in range(height)]
for row in self.room:
print row
def __str__(self):
for row in self.room:
print row
</code></pre>
<p>通过在类上实现<code>__iter__()</code>方法,可以使<code>self</code>iterable:</p>
<pre><code>class Room(object):
...
def __iter__(self):
for x in self.room:
yield x
</code></pre>
<p>将使您的课程变得更加易学:</p>
<pre><code>>>> r =Room()
>>> r
<__main__.Room instance at 0x02A02620>
>>> for x in r:
print(x)
0
1
2
3
4
</code></pre>