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
?
In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties. There are 7 primitive data types: string. number. bigint.
A 'data-type' is a type of data such as an integer, a boolean, or a character. It defines what values a variable of a 'data-type' can hold and what operations can be performed on it. An object is an instance of a class.
The variable can be reassigned a new value but the existing value of the primitive cannot be changed like we do with arrays or objects. So this is one of the main differences between both types: Primitive Types are immutable and Object Types are mutable.
Non-primitive data types are called reference types because they refer to objects.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With