3

In Python, what is the base class for all other classes (real Base), that all other classes inherit from?

Jakub M.
  • 32,471
  • 48
  • 110
  • 179

2 Answers2

4

All new style classes inherit from object.

New-style classes were introduced in Python 2.2 to unify classes and types. Here there is a nice description of what a new style class is. – Paolo Moretti

Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
  • 1
    New-style classes were introduced in Python 2.2 to unify classes and types. [Here](http://docs.python.org/release/2.2.3/whatsnew/sect-rellinks.html) there is a nice description of what a new style class is. – Paolo Moretti Aug 12 '11 at 15:51
1

It is object. At least in 2.7 and 3.1.

>>> class A():
...     pass
... 
>>> isinstance(A, object)
True
utdemir
  • 26,532
  • 10
  • 62
  • 81
  • this surprised me.. how, then, is a `class B(object): pass` different from class A? I get `<__main__.A instance at ...>` and `<__main__.B object at ...>` ! – wim Aug 12 '11 at 15:48
  • It's a compatibility workaround because new-style classes were introduced in the middle of Python 2 and they couldn't break the old functionality. In Python 3 you no longer need to include the explicit `object inheritance. – Katriel Aug 12 '11 at 15:57
  • On Python2, if you inherit from object, you create a new style class. On Python3, all classes are new style( https://gist.github.com/d2ded62e5f885d61ff75 ). And look at http://stackoverflow.com/questions/54867/ for differences between old and new style classes. – utdemir Aug 12 '11 at 16:04
  • 2
    eeeish, what an ugly wrinkle! then again, i guess pythons do have vestigial legs inside their bodies.. – wim Aug 12 '11 at 16:12
  • Indeed it is ugly and that's one of the things Python 3 addresses. – jathanism Aug 12 '11 at 16:40