【发布时间】:2012-01-07 09:37:47
【问题描述】:
我定义了一个包含 return 语句但没有返回值的函数。我的代码如下:
def seed(addy):
# urllib2 stuff is here
seed_result = re.search('<td>Results 1 - \d+ of (\d+)',seed_query) # searches for '<td>Results 1 - x of y', captures 'y'
seed_result = seed_result.group(1) # this is 'y' from above
# there's a call to a different function here which works properly
# other stuff going on here pertaining to addy but seed_result still has my string
# now I want to return the seed_result string...
return seed_result
# ... some code outside of the seed function, then I call seed...
seed(addy)
print "Result is %s" % seed_result
我已经尝试过在函数之外定义和不定义seed_result以“初始化”它,但这对结果没有影响,即我的打印语句最后产生“结果是” - 没有seed_result。我还在 return 语句中将 seed_result 包裹在括号中,尽管我相信我的做法是正确的。括号没有任何区别。
A 在 Python shell 中设置了一个非常基本但类似的函数,并像我在这里所做的那样调用它,但它确实有效。不知道我错过了什么。
感谢您的反馈和指导。
【问题讨论】:
-
尝试执行打印语句来调用导致“结果为无”的函数。 挠头
-
它在 Python shell 中工作的原因是 shell 会打印出任何计算表达式的结果。正如 Jon Skeet 所说,要在您的程序中获取它,您必须将它分配给某个变量。