一开始编码如下:
def __traverse__(self)然后我在 Python 中尝试导入 tree 模块:
"""...
Comments
...
"""
id_self = id(self)
if self.__id_visited.has_key(id_self): return {}
# To avoid cyclic link problem!
Tree.__id_visited[id_self] = None
pathseq = self.__path_stack
yield pathseq, self
...
>>> import tree这个错误实际上就是由于上面使用了 return,对 iterator/generator 来说,必须使用 StopIteration 异常处理来终止!
Traceback (most recent call last):
File "", line 1, in
File "tree.py", line 399
yield pathseq, self
SyntaxError: 'return' with argument inside generator
def __traverse__(self):
id_self = id(self)
if self.__id_visited.has_key(id_self):
# return {}
raise StopIteration
# To avoid cyclic link problem!
Tree.__id_visited[id_self] = None
pathseq = self.__path_stack
yield pathseq, self
......
没有评论:
发表评论