I have a query in class based OOP paradigm aspect of imperative/dynamic python language. Below is the diagram for a class hierarchy in python taken from link .
As per the diagram, I understand that, Every datatype in python is a class. bool
class is direct child of object
class. When we create a new user-defined class( Natalie
) then object
class will be an ultimate ancestor for this class. class objects( int
/ str
/ object
) themselves are instances of type
class. In java world we call it as 'Class' class instead of type
class.
My question is:
1) Is my understanding correct?
2) If yes, Am confused with the arrow direction from type
to object
class. How do i understand this?
3) I could not understand/visualise these two phrases(in brown), Please help me on this.
the object class is an instance of the type class and itself
the type class is an instance of the object class and itself
Your understanding is basically right. type
and object
are special in that they are the base of the type hierarchy. Everything is an instance of object
. Every type/class is an instance of type
. So type
is an instance of object
and object
is also an instance of type
.
This means that the inheritance relationship cannot really be represented as a tree, because there is a cycle: type
and object
are instances of each other. This kind of mutual inheritance is not normally possible, but that's the way it is for these fundamental types in Python: they break the rules. The diagram is somewhat misleading since it shows type
as inheriting from object
but not vice versa. Really, the arrow between type
and object
should be double-headed, indicating the inheritance goes both ways.
It's important to distinguish between what types/classes inherit from and what they are instances of, although this isn't directly represented in that diagram. A class or type (like int
or Natalie
) is a subclass of object
, but it is an instance of type
. The two statements that you refer to in Question 3 relate to this. The object
type is an instance of object
, because everything is an object; object
is also an instance of type
, because object
is a type (aka a class). Likewise, type
is an instance of object
, because everything is an object; and type
is also an instance of type
, because type
is a type (it is the type of types, and the type of user-defined classes).
There is also an inaccuracy in the diagram: bool
is not actually a direct subclass of object
, rather it is a subclass of int
(which is a direct subclass of object
).
print (isinstance(type,object))
print (type.__class__)
print (isinstance(object,type))
I dont know if that will help you more than the object but there is some empirical proof
In Java Object class is the super class of every class and single inheritance is applicable, but in python it is not true.
In Java
in Python3, before inheriting from object
After inheriting object
So when you are creating a class in pythyon it is not inheriting from object. It is basically follows modular architecture. Not like java's Tree structure.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.