我试图通过以下代码实验性地确定Python的最大递归深度:
def recursive(i):
i = i + 1
try:
recursive(i)
except RuntimeError:
print 'max depth == %d' % i
exit(0)
recursive(0)
Run Code Online (Sandbox Code Playgroud)
但当我运行它时,发生了这种情况:
[ hive ~ ]$ python recursive.py
max depth == 999
max depth == 998
max depth == 997
max depth == 996
max depth == 995
max depth == 994
Run Code Online (Sandbox Code Playgroud)
为什么我的程序RuntimeError在第一次遇到时不会立即退出,但是继续再运行5次recursive()?
我有一个像 REST API 一样使用的小型 Flask 应用程序,它实现了许多这样的方法:
@route('/do/something', methods=['POST'])
def something():
app.logger.debug("got request to /do/something")
result = something_implementation(request.json())
app.logger.debug("result was %s", str(result))
return flask.Response(result)
Run Code Online (Sandbox Code Playgroud)
我想用来自请求对象的信息自动丰富这些日志消息,例如标头信息、正文部分以及其他任何方便的信息。
我如何以优雅和 Pythonic 的方式做到这一点?
当然,我可以将 app.logger 包装在我自己的函数中并传递请求对象和消息,但必须有一种更好、更简单的方法,我错过了。