Skip to main content
 首页 » 编程设计

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

2025年05月04日11lyj

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

我有一个名为 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...