1

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.

class hierarchy

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
overexchange
  • 15,768
  • 30
  • 152
  • 347

3 Answers3

9

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).

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • brenban inheritance goes both ways, how do i understand this? – overexchange Apr 07 '14 at 19:47
  • @Sham: I don't understand what you're asking. What do you mean "how do you understand it"? – BrenBarn Apr 07 '14 at 19:48
  • brenban inheritance is genrally top down from what i saw in java, so how can it be two ways? – overexchange Apr 07 '14 at 19:52
  • @Sham: That's what I'm saying. This particular inheritance relation (between `type` and `object`) in Python is special and breaks the normal rules. – BrenBarn Apr 07 '14 at 19:53
  • brenban can you also clarify the meaning of two statements in Q3 of my query? What does instance of itself mean? – overexchange Apr 08 '14 at 04:35
  • @Sham: I added a bit to my answer. – BrenBarn Apr 08 '14 at 04:38
  • Hi Bran, One doubt from your answer- "type is an instance of object" : We have instances for classes, do you mean `object` is a class? and `type` is getting instantiated from it. This I couldn't able to understand. – METALHEAD Jan 02 '20 at 20:49
  • @METALHEAD: Types and classes are basically the same thing. "instance of" means the same thing for types as it does for classes. Being an instance of a type is the same as being an instance of a class. Beyond that I'm not sure what it is you don't understand. – BrenBarn Jan 03 '20 at 18:47
1
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

subhashis
  • 4,629
  • 8
  • 37
  • 52
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • This was a good suggestion. Could you clarify? I got type for for both print(type.__class__) and print(object.__class__). Why isn't print(type.__class__) == ? I thought that if the relationship between type and object is two-way, we would get that answer. What have I misunderstood? – heretoinfinity Apr 03 '18 at 23:13
-1

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

enter image description here

in Python3, before inheriting from object

enter image description here

After inheriting object

enter image description here

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.

subhashis
  • 4,629
  • 8
  • 37
  • 52