What are Python's type "objects" exactly?
python
Solution
I'll answer the 1,2 question first, then 4th then 3rd:
- "Whats the relationship between a type type "objects" and "class instances" type objects?"
- "Can I assume the ~meta API to in-built type objects is the same as that of "class instance" type objects?"
They are the same, and yes they share a common API. When the documentation describes built in types as "objects", or class instances as "objects", or a class or whatever as an "object" ... they are talking about exactly the same language construct.
- "In general, what are "objects" ..."
The object is a foundational language feature in Python that supports attributes and behaviors much like other OOPLs. All Python objects also have a class much like other classed based OOPLs. The `object` class is the base of the class hierarchy in Python. Thus all classes are subclasses of the `object` class, and all the aforementioned "objects" and instances of `object` - by way of inheritance.
It's worth first pointing out explicitly that in Python (2.2 and above) "type" and "class" mean the same thing (for all intents and purposes). So "int", and the rest of the so called builtin types are classes (which are represented as objects of course). For example this `x = int(1)` calls the `int` class (object) to construct an int instance object, x.
It's true to say there are two types of object in Python; "type" objects, or those that represent types, and "non-type" objects - those that don't. But it's equally true to say there are two type of integers; zero, and not zero. It doesn't mean much: Everything in Python is an object including classes. Since classes form a kind object, they are all instances of a class called "type". The type object is also an instance of type. Note you can inspect the inheritance hierarchy of class by examining the _bases_ attribute of a class object. In all cases it leads back to the `object` class - of course. See https://www.eecg.utoronto.ca/~jzhu/csc326/readings/metaclass-class-instance.pdf for further details on this.
- "...where is it all documented?"
Well, that's actually a good question. It should be covered in the Data Model section of the language reference, but it is sort of skimmed over. The constructor for object objects, object (that made sense) is a built in and documented with the rest of the builtins here. Also the Classes chapter of The Python Tutorial also covers this area a bit.
Problem
I'm just beginning to learn Python. I'm finding the type system a little hard to understand. I have a number of questions, but primarily and to cut a long story short; The documentation states: "All data in a Python program is represented by objects ... Every object has an identity, a type and a value." No problem. But beyond that its not really described what "objects" are. For example the docs don't even cover that these "objects" support a dot operator - from my PoV they could be some in memory data structure not exposed to the user beyond `id()`, `type()` etc. However I gather there is some underlying meta object interface similar to that described for the class instance type object in the docs. To work from an example: If I do this on a class instance "x": ``` x.__class__.__name__ ``` I get the name of its class. I understand that. The documentation describes the `__class__` and `__name__` properties of class instances and class type objects. If I do this `[].__class__.__name__` I get "list". Similarly `int(1).__class__.__name__` gives "int". Its ambiguous to me exactly what is going on under the hood, and I would like clarification. So my questions are: - Whats the relationship between a type type "objects" and "class instances" type objects? - Can I assume the ~meta API to in-built type objects is the same as that of "class instance" type objects? - If so, what is this interface and where is it documented? - In general, what are "objects" that correspond to built-in types, and how are they implemented?