In Python, when subclassing tuple, the __new__
function is called with self as an argument. For example, here is a paraphrased version of PySpark's Row
class:
class Row(tuple):
def __new__(self, args):
return tuple.__new__(self, args)
But help(tuple)
shows no self
argument to __new__
:
__new__(*args, **kwargs) from builtins.type
Create and return a new object. See help(type) for accurate signature.
and help(type)
just says the same thing:
__new__(*args, **kwargs)
Create and return a new object. See help(type) for accurate signature.
So how does self
get passed to __new__
in the Row
class definition?
- Is it via
*args
? - Does
__new__
have some subtlety where its signature can change with context? - Or, is the documentation mistaken?
Is it possible to view the source of tuple.__new__
so I can see the answer for myself?
My question is not a duplicate of this one because in that question, all discussion refers to __new__
methods that explicitly have self
or cls
as first argument. I'm trying to understand
- Why the
tuple.__new__
method does not haveself
orcls
as first argument. - How I might go about examining the source code of the tuple class, to see for myself what's really going on.
Follow-up: Moderators closed this old question as a duplicate of this one. But it's not a duplicate. Look at the accepted answer on this question and note how little overlap it has with the answers in the claimed duplicate, in terms of the information provided.