Python 异常 - args 属性如何自动设置?

2024-05-24

假设我定义了以下异常:

>>> class MyError(Exception):
...     def __init__(self, arg1):
...         pass

然后我实例化该类以创建异常对象:

>>> e = MyError('abc')
>>> e.args
('abc',)

这里是如何args属性正在设置? (在里面__init__, 我没做什么。)


args被实现为一个数据描述符__get__ and __set__方法。

这发生在内部BaseException.__new__就像@bakatrouble提到的那样。除此之外,里面发生了什么BaseException.__new__大致类似于下面的Python代码:

class BaseException:
    def __new__(cls, *args): 
        # self = create object of type cls
        self.args = args  # This calls: BaseException.args.__set__(self, args) 
        ...
        return self

在C代码中Python 3.7.0 阿尔法 1,上面的Python代码看起来像这样(检查 Python 的 C 代码是否有任何过去或未来的差异):

BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    # other things omitted... 
    self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
    # many things follow... 
    if (args) {
        self->args = args;
        Py_INCREF(args);
        return (PyObject *)self;

    }
    # many more things follow
}

交互实验:

>>> e = Exception('aaa')
>>> e
Exception('aaa',)

>>> BaseException.args.__set__(e, ('bbb',))
>>> e
Exception('bbb',)
>>> BaseException.args.__get__(e)
('bbb',)

因此,神奇的灵感args让你的眼睛望向天堂的事情发生在BaseException.__new__,当一个对象BaseException或其任何子类被创建。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 异常 - args 属性如何自动设置? 的相关文章

随机推荐