To begin with super()
in itself is simply shorthand for super(A, B)
, where A
is the class wherein the code occurs, and B
is the first argument to the function in which the code occurs; so in your particular case, super().__new__(cls)
expands to super(CarModel, cls).__new__(cls)
.
In turn, super(T, O)
returns a "super object". To understand what a super object does, you need to understand how attribute references on instances and classes work in Python.
Assuming no __getattr__
or __getattribute__
methods are involved, referencing attribute A
on an object O
(that is, evaluating O.A
or getattr(O, "A")
) proceeds through the following steps:
- If
"A"
is defined in O
's instance dict (O.__dict__
), then the value on that dict is returned directly, precisely as it is.
- Otherwise, each of the classes in
O
's method resolution order are checked in turn, looking for "A"
in each of their dicts. If found, call the value D
.
- If
D
, in turn, does not define a __get__
, then it is returned as it is. If it does, however, then D
is referred to as a "descriptor", and its __get__
method is called with O
as the first argument, and type(O)
as the second argument.
An attribute reference on a class works about the same, substituting the class being reference for the instance, with the following differences:
- Step 1 doesn't apply.
- The
__get__
method is called with None
as the first argument, and the class being referenced as the second.
Python uses descriptors to implement such things as instance methods, class methods, static methods, and properties.
A super object created with super(T, O)
, then, is a (built-in) object with a __getattribute__
method which is called on every attribute reference on it, and looks up the attribute in the dicts of the only classes following T in O
's MRO. The value it then finds, it calls __get__
on as usual.
The process is a bit complex, so as an example, here's how it would work on your specific case. Since CarModel
is defined as it is, its MRO is [CarModel, object]
.
super().__new__(cls)
expands to super(CarModel, cls).__new__(cls)
, as described above.
super(CarModel, cls)
is evaluated to yield a super object S
.
- Python fetches the attribute
"__new__"
on S
(the equivalent of calling getattr(S, "__new__")
in Python code).
- Since
S
was created on the CarModel
class, it considers the classes following CarModel
in the MRO of CarModel
, and finds "__new__"
in the dict of the object
class itself. Its value, a static method, has a __get__
method, which is called with the arguments None
and cls
. Since __new__
is a static method, its __get__
method simply returns the function as it is, unmodified. Therefore, super(CarModel, cls).__new__
is precisely the same as object.__new__
.
- The function obtained in the last step (that is,
object.__new__
) is called with the cls
argument, where cls
is probably CarModel
, finally a new instance of the CarModel
class.
I hope that was at all understandable.
(For the sake of completeness, it should be mentioned that the actual __new__
function on the object
class is not actually a static method, but a special built-in function that simply has no __get__
method at all, but since the __get__
method on static methods just return the function they were defined with, the effect is the same.)