fbm*_*bmd 4 python coding-style
我有一个该结构的长Python函数:
def the_function(lots, of, arguments):
return_value = None
if some_important_condition:
# a lot of stuff here
return_value = "some value"
else:
# even more stuff here
return_value = "some other value"
return return_value
Run Code Online (Sandbox Code Playgroud)
一个问题是if该else块和块都包含多个屏幕代码.很容易忘记压痕,或者不得不向上滚动以查看我们目前处于什么状态.
改进这一点的一个想法是将其分成几个函数:
def case_true(lots, of, arguments):
# a lot of stuff here
return "some value"
def case_false(lots, of, arguments):
# even more stuff here
return "some other value"
def the_function(lots, of, arguments):
return_value = None
if some_important_condition:
return_value = case_true(lots, of, arguments)
else:
return_value = case_false(lots, of, arguments)
return return_value
Run Code Online (Sandbox Code Playgroud)
但考虑到争论的争论,我不确定这是否可以解决问题.
另一个想法是使用多个退出点:
def the_function(lots, of, arguments):
if some_important_condition:
# a lot of stuff here
return "some value"
# even more stuff here
return "some other value"
Run Code Online (Sandbox Code Playgroud)
但是有几种编码风格可以针对多个退出点提出建议,特别是当它们分开时.
问题是:什么是一种首选的pythonic方法,使原始结构更易于阅读和维护?