gpt4 book ai didi

python - 如果错误,执行此操作并返回,否则继续在一行中执行

转载 作者:行者123 更新时间:2023-11-30 22:24:43 24 4
gpt4 key购买 nike

我的代码中有很多重复,例如这个 block :

if err:
print('Could not parse text!')
print('Error code={}'.format(err.code))
print('Error message={}'.format(err.message))
return err.code

我想让它看起来更好,也许只需一行代码。

所以我想命令编译器在一行中完成此操作:

如果有错误,则打印必要的信息并返回错误代码,否则继续执行。

类似这样的事情:

def error_output(err, text):
print(text)
print('Error code={}'.format(err.code))
print('Error message={}'.format(err.message))
return err.code

return_if(err, error_output, 'Parse error')

尝试过这个:

return error_output(err,'parse error') if err else continue

但是当然不可能像这样使用continue

最佳答案

怎么样:

if err: return error_output(err, 'parse error') 
# more code here

关于python - 如果错误,执行此操作并返回,否则继续在一行中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47763139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com