5

From what I understand of Python 3, the type type is the meta-class that is used to create Class objects, so type is to object as "Car" is to "Jaguar", through a process of calling its __call__ method which then calls the class's __new__ and __init__ methods, returning the instance of the class itself.

At the normal level, the relation between type and object makes perfect sense; everything is an object (subclasses object), and everything has a type; the type of the object 5 is int, and int's type is type itself. Hence isinstance(5, int) and isinstance(int, type) are true. However, isinstance(5, type) is not true since 5 is not a class object, it's an instance of a class.

This article: http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html#object-type-example

Hence defines an object as having exactly one type. But at the very primitive level, of the classes type and object themselves, the relationship between them confuses me. type is a subclass of object, as seen in its __bases__, but object itself is an instance of type.

If describing something like 5 can be split into "meat" and "bones", where "meat" represents the actual creation of it in memory and assigning values to it, and "bones" the definition of how it should be, of its legal range of values and behaviours, does this mean that, at the very core, type is responsible for creating the "meat" of class objects, and object is responsible for defining the "bones"? If so, what is the "meat" equivalent of creating an instance of a class, if the "bones", in this case, is the class int?

2
  • 1
    object is really just the common set of properties all objects in Python support, so I would call object the "bare bones" of an instance/class/metaclass. A class (like int) is really just an extension of type one level down. type dictates what you can and cannot do with a class while the class dictates what you can and cannot do with an instance. Commented Feb 25, 2017 at 14:21
  • Right, but is it possible to put in attributes in meta-classes which affect instances of the class they created? Commented Feb 25, 2017 at 15:38

1 Answer 1

6

Every class of objects in Python inherits the behaviours of the object class. Firstly, it means that

>>> isinstance(x, object)
True

will be true in Python 3 no matter what x happens to be bound to. It also means that everything will also inherit the methods of the object as such, unless overridden by a more specific class. Thus, x == y will resort to object.__eq__ if neither of x or y overrode the __eq__ method.

type in Python 3 is also an object - it inherits from object:

>>> isinstance(type, object)
True

It means that type is an object, and it has inherited the behaviour from the object class. That is what enables one to write things like

>>> type == type
True
>>> type.__eq__
<slot wrapper '__eq__' of 'object' objects>

The type.__eq__ was inherited from the object class.

But the object class itself is also a type:

>>> isinstance(object, type)
True

It means that the class object as an object inherits the behaviour of type - type is object's metaclass.

And of course type's metaclass is type:

>>> isinstance(type, type)
True

And the class object is an object too:

>>> isinstance(object, object)
True

When you construct an instance of class, the class itself is responsible for both the meat and bones for that class, but perhaps one could put the distinction in that the bones follows the ordinary class inheritance, and meat is split between the class and the metaclass lineage; and the metaclass is also the bones for the class object itself.

Sign up to request clarification or add additional context in comments.

6 Comments

In drawing that distinction does it mean that despite any meta class lineage (by default, builtin, or custom meta-classes) only defining the instantiation of the class object itself, they still act to outline the template of each instance? May I clarify if meta classes can define methods to which their respective class objects and class instances may call upon?
meta classes can define methods that the class objects can call; and they can extend the class object itself with methods that the instances have, but not directly. Because an instance of a class is not an instance of the metaclass.
i.e. "Mercato" is a "Human" and a "Human" is a "Species" does not mean that "Mercato" is a "Species".
"and they can extend the class object itself with methods that the instances have, but not directly" could you clarify on that? Can the instances call on such methods?
the question here is not "can they be called or not" but whether they readily show on the instances.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.