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
True
if 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
object
is 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
object
is a base for all classes.
https://docs.python.org/3.9/library/functions.html?highlight=object#object
type
inherits fromobject
(https://docs.python.org/3.9/library/functions.html?highlight=object#type) sotype
is both a subclass and also an instance ofobject
using theisinstance
description.
2) object
is an instance of type
object
is a type object. In other words,object
is 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 type
type(type) # returns type
object.__class__ # returns type
type.__class__ # returns type
Which helps us understand how
isInstance(object, type)
will returnTrue
.An intuitive way to think about this is that the
object
object is of typetype
butobject
is not a subclass oftype
because it doesn't inherit fromtype