What are Python's type "objects" exactly?

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 http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html 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.


It's a bit hard to understand what you are asking.

A type is the class of a class. Like everything else in Python, classes themselves are objects, and you can pass them around, assign them to variables, etc. If you ask a class what its class is, you will get the answer type. If you ask a class instance what its class is, you will of course get the class.

>>> type(int)
<type 'type'>
>>> type(1)
<type 'int'>

>>> class Foo(object):
...   pass
>>> type(Foo)
<type 'type'>
>>> obj = Foo()
>>> type(obj)
<class '__main__.Foo'>

(here the function type(x) is another way of doing x.__class__.)

Tags:

Python