为什么异常处理不检查库的存在
我不明白为什么 python(至少 2.7)不验证错误的异常处理机制。
示例:
try:
some code selecting data from pymongo
except pymongo.errors.OperationFailure:
exception
在这种情况下,如果不是第一次调用异常,python 将不会验证我是否确实导入了 pymongo lib。
知道为什么吗?
I don't understand why python (at least 2.7) is not validating the exception handling mechanism for errors.
Example:
try:
some code selecting data from pymongo
except pymongo.errors.OperationFailure:
exception
In this case, if the exception is not called for the first time, python will not validate if I actually did import the pymongo lib.
Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我正确地阅读了您的问题,您想知道为什么
except pymongo.errors.OperationFailure 在加载模块时如果您尚未导入
pymongo 则不会导致错误模块。
与 Python 中的大多数事物一样,
except
子句的参数在运行时评估。事实上,它们可以是表达式! Python 在“编译”时不会验证它们,就像它当时不会验证任何其他名称一样。原因是Python是一种动态语言。导入可以有条件地完成,或者根据“编译”时未知的名称执行,并且模块和其他命名空间可以通过代码替换、修改或删除。因此,在不运行代码的情况下,Python 实际上无法知道 pymongo.errors.OperationFailure 是否是代码执行时的有效名称。
If I'm reading your question right, you want to know why
except pymongo.errors.OperationFailure
doesn't cause an error when the module is loaded if you haven't already imported thepymongo
module.Like most things in Python, the arguments to
except
clauses are evaluated at runtime. In fact, they can be expressions! Python does not validate them at "compile" time any more than it validates any other names at that time.The reason is that Python is a dynamic language. Imports can be done conditionally, or performed based on names that are not known at "compile" time, and modules and other namespaces can be replaced, modified, or removed by code. As a result, Python literally cannot know whether
pymongo.errors.OperationFailure
is a valid name at that point in your code's execution without running your code.根据 PyMongo 文档,异常
pymongo.errors.OperationFailure
将“在数据库操作失败时引发”。因此,只有在引发此类错误时才会评估您的except
块。我假设通过“验证 pymongo 的存在”您指的是这样的东西:
此方法经常用于提供后备(和向后兼容性)而不是“验证”导入。例如,在
json
编码器/解码器的情况下,我们可以尝试是否有可用的simplejson
库,并使用json
库作为后备,如下所示:假设在脚本的开头,您已经有了
import pymongo
,我不认为您应该检查或“验证”pymongo 是否已导入:import如果找不到
将会引发pymongo
库,pymongoImportError
。According to PyMongo documentation, exception
pymongo.errors.OperationFailure
will be "raised when a database operation fails". AS such, yourexcept
block gets evaluated only when such an error is raised.I'm assuming that by "validation of pymongo's existence" you are referring to somethine like:
This method is often used to provide fallbacks (and backwards compatibity) not to "validate" imports. For instance in the case of
json
encoder/decoder, we can try whether we havesimplejson
library available and usejson
library as a fallback as follows:Assuming that in the beginning of your script, you already have
import pymongo
, I don't see a reason why you should be checking or "validating" that pymongo has been imported:import pymongo
will already raise anImportError
ifpymongo
library is not found.首先,
pymongo.errors.OperationFailure
可以在任何地方定义,不仅可以作为pymongo
模块的一部分,还可以作为在同一文件中定义的 pymongo 对象的属性。因此,在处理异常时,Python 不应检查特定模块是否已导入。
但是如果你这样做:
你会看到如果找不到模块,实际上会引发导入错误:
如果我的答案对你来说还不够,并且你想了解更多关于为什么包含
pymongo.errors.OperationFailure
当您第一次运行脚本时,即使您的代码中没有任何import pymongo
语句,也不会抛出任何错误,然后请参阅<一个href="https://stackoverflow.com/questions/8331267/why-the-exception-handling-is-not-checking-existence-of-library/8331349#8331349">kindall关于Python是一种动态语言的回答。First of all,
pymongo.errors.OperationFailure
may be defined anywhere, not only as a part ofpymongo
module, but also as a property of property ofpymongo
object defined in the same file.Thus when handling exceptions Python should not check if specific module has been imported.
But if you do something like that:
you will see that import error is actually raised if module is not found:
If my answer is not enough for you and you want to know more about why inclusion of
pymongo.errors.OperationFailure
does not throw any error when you run your script for the first time, even though you do not have anyimport pymongo
statement in your code, then please see kindall's answer on Python being a dynamic language.