python - types.py 中的这段代码有什么作用?

标签 python

try:
    raise TypeError
except TypeError:
    try:
        tb = sys.exc_info()[2]
        TracebackType = type(tb)
        FrameType = type(tb.tb_frame)
    except AttributeError:
        # In the restricted environment, exc_info returns (None, None,
        # None) Then, tb.tb_frame gives an attribute error
        pass
    tb = None; del tb

我完全看不懂这段代码。它的用途是什么?

最佳答案

获取一个traceback 对象和一个frame 对象是一个技巧,这样TracebackType 和FrameType 就可以分配它们的类型。它只是引发异常,以便捕获异常,然后从 sys.exc_info 获取回溯和帧。

关于python - types.py 中的这段代码有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1990339/

相关文章:

python - 惰性延迟列表达到最大递归深度

c++ - 如何使用 boost::python 将 std::pair 公开给 python?

python - scikit learn 类型错误 only integer arrays with one element can be converted to an index

python - 动画二次网格变化(matshow)

python - Pandas groupby 忽略某些行值

python - Scipy - z 值的两个尾部 ppf 函数?

python - 以下三种方式在构造函数中声明属性有什么区别?

python - 获取 "Message: h is null"

python - 如何在 Ubuntu Linux 上将 pymssql 安装到 Python 3.4 而不是 2.7?

python - 无法在python中提取电子邮件文件的正文