假设我想创建一个接受迭代的函数。该可迭代对象可能包含任何级别的其他可迭代对象。我想创建一个按顺序遍历这些的函数。例如:
import collections
def it(l):
  for i in l:
    if isinstance(i, collections.Iterable):
      it(i) 
    else:
      print i
it([ [1, 2, 3], [[4, [5, 6]], 7], 8, [9, 10]])  
这会产生以下输出(如预期的那样):1 2 3 4 5 6 7 8 9 10
不应该我想用发电机来做这个。为什么以下工作不像我期望的那样工作(基本上用产量替换打印语句):
import collections
def it(l):
  for i in l:
    if isinstance(i, collections.Iterable):
      it(i) 
    else:
      yield i
谢谢!