gpt4 book ai didi

python - python 中的最佳返回类型策略

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:53 25 4
gpt4 key购买 nike

我正在为下一个问题寻找一种“按预期方式完成”的解决方案:

我有一个名为 InputChecker 的类,它接受一个输入文件,检查它的内容,如果一切正常,则将文件返回给下一个类(ExpofitAgent ) 以采取进一步行动。

但是,如果文件的格式不正确,InputChecker 会生成一条错误消息,说明在文件的哪一行发现了错误。

InputChecker 不打印错误,它只是生成稍后打印的消息。

因为我是在 python 中做的,所以我的方法可以返回一个文件或一个字符串。如何在不需要包含额外标志的情况下检查文件是否正常?

最佳答案

引发异常:http://docs.python.org/tutorial/errors.html

Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal... Most exceptions are not handled by programs, however, and result in error messages...

关于python - python 中的最佳返回类型策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9979735/

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