日益复杂/压痕嵌套的try-S我有这样的代码:如何避免在Python
try:
request=parse_request
except:
print "cannot parse your malformed request"
exit()
else:
try:
fh=a_factory_function()
except:
print "cannot create object"
else:
if request['operation']=='search':
pass
elif request['operation']=='more_like_this':
pass
elif request['operation']=='list_files':
pass
elif request['operation']=='update':
pass
else:
print 'unsupported operation'
在目前的形式,它具有压痕
- 两个层面解析请求
- 工厂函数生成一个对象来处理请求
我可以轻松地imagin这将会达到4个级别,并且对于我们的弱点推理而言过于复杂。是否有Pythonic的方式来压扁缩进,并使其“线性”?
似乎对我来说是最合理和pythonic。 – Glider