Why does python's Exception's repr keep track of passed object's to __init__?
When a Python object is created, it is the class's __new__
method that is called, and __init__
is then called on the new instance that the __new__
method returns (assuming it returned a new instance, which sometimes it doesn't).
Your overridden __init__
method doesn't keep a reference to b
, but you didn't override __new__
, so you inherit the __new__
method defined here (CPython source link):
static PyObject *BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds){ // ... if (args) { self->args = args; Py_INCREF(args); return (PyObject *)self; } // ...}
I omitted the parts that are not relevant. As you can see, the __new__
method of the BaseException
class stores a reference to the tuple of arguments used when creating the exception, and this tuple is therefore available for the __repr__
method to print a reference to the objects used to instantiate the exception. So it's this tuple which retains a reference to the original argument b
. This is consistent with the general expectation that repr
should return Python code which would create an object in the same state, if it can.
Note that it is only args
, not kwds
, which has this behaviour; the __new__
method doesn't store a reference to kwds
and __repr__
doesn't print it, so we should expect not to see the same behaviour if the constructor is called with a keyword argument instead of a positional argument. Indeed, that is what we observe:
>>> A(B())A(<__main__.B object at 0x7fa8e7a23860>,)>>> A(b=B())A()
A bit strange since the two A
objects are supposed to have the same state, but that's how the code is written, anyway.
Alright, I think I've found the trick. Here's the C source code, but I'll reimplement something similar in Python to demonstrate.
In addition to the usual __init__
(which you're overriding), Python also has a magic method called __new__
. When you construct an A
as A(B())
, it's doing something roughly like
b = B()a = A.__new__(A, b)a.__init__(b)
Now, you've overridden A.__init__
, so Exception.__init__
never gets called. But A.__new__
is simply Exception.__new__
(more precisely, it's BaseException.__new__
, which is essentially the C source code I linked). And, based on the linked code, that's roughly
class BaseException: def __new__(cls, *args): obj = object.__new__(cls) obj.args = args return obj
So we explicitly store the arguments tuple in a field called args
on the exception object. This is the actual arguments tuple passed to the constructor, even if we override __init__
. So repr
is just referencing self.args
to get the original arguments back.
Note that I'm being a bit imprecise here. If you check BaseException.__new__
in the REPL, you'll see that it's still object.__new__
. The C callbacks work differently and use some compiler magic we don't have access to, but the basic idea is the same.
We can explore the difference with Ipython's tab complete.
In [230]: a=A(B())In [231]: aOut[231]: __main__.A(<__main__.B at 0x7f29cd1e71c0>)
With ipython tab complete:
In [234]: a. args a.bin message a.npy with_traceback() a.t1 In [234]: a.argsOut[234]: (<__main__.B at 0x7f29cd1e71c0>,)
The class without Exception, only has one attribute, message
:
In [235]: a1=A1(B())In [236]: a1Out[236]: <__main__.A1 at 0x7f29ccc21a60>In [237]: a1.messageOut[237]: 'hello'
and a plain Exception:
In [238]: e=Exception(B())In [239]: eOut[239]: Exception(<__main__.B at 0x7f29ccd0e790>)In [240]: e. args with_traceback()
And using a
as an exception:
In [240]: raise(a)Traceback (most recent call last): File "<ipython-input-240-cf5e5bb7d43e>", line 1, in <module> raise(a)A: <__main__.B object at 0x7f29cd1e71c0>