Show class relation between type and object:
issubclass(type,object)
True
issubclass(object,type)
False
It is clear that type derived from object,object is the father class of type,type is the son class of object.
isinstance(type,object)
True
isinstance(object,type)
True
How to understand that type is the instance of object and vice versa ?
CodePudding user response:
Useful Definition
isinstance(object, classinfo)
Return
Trueif the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof.
https://docs.python.org/3.9/library/functions.html?highlight=isinstance#isinstance
1) type is an instance of object
- All data is represented by objects and
objectis a base for all classes
Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects.
https://docs.python.org/3.9/reference/datamodel.html#types
objectis a base for all classes.
https://docs.python.org/3.9/library/functions.html?highlight=object#object
typeinherits fromobject(https://docs.python.org/3.9/library/functions.html?highlight=object#type) sotypeis both a subclass and also an instance ofobjectusing theisinstancedescription.
2) object is an instance of type
objectis a type object. In other words,objectis of type,type
Type Objects
Type objects represent the various object types. An object’s type is accessed by the built-in functiontype()
https://docs.python.org/3.9/library/stdtypes.html?highlight=subclass#type-objects
type(object) # returns typetype(type) # returns typeobject.__class__ # returns typetype.__class__ # returns typeWhich helps us understand how
isInstance(object, type)will returnTrue.An intuitive way to think about this is that the
objectobject is of typetypebutobjectis not a subclass oftypebecause it doesn't inherit fromtype
