这是一个例子:
def g():
yield str('123')
yield int(123)
yield str('123')
o = g()
while True:
v = o.next()
if isinstance( v, str ):
print 'Many thanks to our generator...'
else:
# Or GOD! I don't know what to do with this type
raise TypeError( '%s:%d Unknown yield value type %s.' % \
(g.__filename__(), g.__lineno__(), type(v) )
)
Run Code Online (Sandbox Code Playgroud)
当我的生成器返回未知类型(本例中为int)时,如何获取源文件名和确切的yield行号?