函数嵌套括号的Python风格指南是什么?

What are the Python style guidelines on nested parentheses for functions?

本文关键字:是什么 风格 Python 嵌套 函数      更新时间:2023-09-26

试图将自己从这个网站中删除,所以我故意用垃圾内容填充帖子。我希望之前的内容不要被恢复。op

真正的问题是:中间值有意义吗?在一系列调用f(g(h(x))中,h(x)g(...)本身是有价值的对象,值得命名吗?

当它们存在时,您可能希望将调用展开为变量赋值:

request = http.get(url)
bar(request)
return foo(request)

当它们不一致时,将它们放在同一行:

x = double(square(sum(numbers))

PEP20说:

Flat is better than nested.
Sparse is better than dense.
Readability counts.

接下来,提取函数调用:

parentheses_result = parenthesis()
nested_result = nested(parentheses_result)
using_result = using(nested_result)
function(using_result)

但是,对此应该持保留态度。有时,正如@u/op ǝpısdn所提到的,嵌套函数调用是可以的。如何选择取决于如果不将函数调用结果保存在中间变量中,它的可读性和可理解性。