TL;DR - probably not. But I tried.
This is really weird and feels like turtles all the way down. I've actually not delved into this arena very much before, though it's something that sounded fun and powerful. This explanation was confusing, and so was the rest of the information on that page, but I feel like I have some enlightenment. Whether or not I can explain that clearly, I'm not sure, but I'll have a go.
Let's look at the turtles, first:
>>> isinstance(type, object)
True
>>> isinstance(object, type)
True
Wait, what?
How is object
an instance of type
, when type
is an instance of object
? That feels like saying something like:
class Parrot: pass
ex = Parrot()
isinstance(ex, Parrot)
isinstance(Parrot, ex)
Should be True
both times. But obviously it's not. Even (as Tadhg McDonald-Jensen pointed out)
>>> isinstance(type, type)
True
This should indicate to you that there is some magic going on behind the scenes. So at this point, let's just completely forget about Python (I know, why would we ever want to do such a horrible thing?)
In general, all computer programs are are 1's and 0's (and more accurately they're just a bunch of logic gates and electrons at >~2.5v and ~<2.5v, but 0's and 1's are good enough). Whether you wrote it in assembly, actual machine code, Python, C#, Java, Perl, whatever - they're all just bits.
If you write a class definition, that class is just bits. An instance of that class is just more bits. And a programming language and a compiler and an interpreter is just even more bits.
In the case of Python, it's the python
interpreter that gives meaning to the bits that are our Python programs. As an interesting point, a lot of what we typically consider to be Python is actually written in Python (though most of it is C, for us CPython folks, Java for Jython, etc.).
So now we come to this thing we call type
and object
. As the article points out, they're kind of special. So, we know that we can create a class, and then that class is an object:
>>> class Confusion: pass
...
>>> isinstance(Confusion, object)
Which makes sense, if you think about it - you may have created class-level variables:
>>> class Counter:
... count = 0
... def __init__(self):
... Counter.count += 1
... print(self.count)
...
>>> Counter()
1
<__main__.Counter object at 0x7fa03fca4518>
>>> Counter()
2
<__main__.Counter object at 0x7fa03fca4470>
>>> Counter()
3
<__main__.Counter object at 0x7fa03fca4518>
>>> Counter()
4
<__main__.Counter object at 0x7fa03fca4470>
>>> Counter.count
4
>>> Counter.__repr__(Counter)
'<type object at 0x1199738>'
But as this last example shows (and is mentioned in the post), a class declaration, what you get with class SomeClass: pass
, that declaration of a class is actually an instance of another class. In particular, it's an instance of the type
class. And that instance (which we call a class) when called will produce an instance of itself:
>>> Counter.__call__()
5
<__main__.Counter object at 0x7fa03fca4518>
So what does all this have to do with the relationship between type
and object
?
Well, somewhere, python
creates a series of bits that is object
, and a series of bits that is type
, and then wires them together in such a way that
>>> type.__bases__
(<class 'object'>,)
>>> object.__bases__
()
Because I currently don't feel like looking through the source, I'm going to make a guess that type
is created first, and object
is produced from that type and that type.__bases__
is set to (class 'object')
. By creating this circular relationships between type
and object
, it gives the appearance that it's just turtles all the way down, when really the last two turtles are just standing on top of each other.
I don't think there's really a better way to explain what's going on here than how the article describes it - at least in a classical OOP is-a/has-a style of thinking, because it's not actually that sort of thing. Like trying to plot a 3d figure in 2d space, you're going to have problems.
It's just two sets of bits that have some bits inside them that happen to be the address of one another.