0
>>> class MyKlass:
...     pass
... 
>>> 
>>> 
>>> a = MyKlass()
>>> 
>>> type(a)
<type 'instance'>
>>> type(MyKlass)
<type 'classobj'>
>>> 
>>> 
>>> class MyKlass(object):
...     pass
... 
>>> 
>>> a = MyKlass()
>>> 
>>> type(a)
<class '__main__.MyKlass'>
>>> type(MyKlass)
<type 'type'>
>>> 

In my above code, one class is not inherited from any base class and the other is inherited from object base class.

I have read somewhere if you do not inherit explicitly, the default parent class is object, am I right?

But if default is object, why type to both class is different? When and how these above different behaviour is useful?

horro
  • 1,262
  • 3
  • 20
  • 37
Pravin
  • 677
  • 2
  • 7
  • 22
  • 1
    The difference between old style and new style classes exists only in Python 2.x. – guidot Jun 12 '17 at 11:22
  • 1
    From the first sentence of [this page](https://wiki.python.org/moin/NewClassVsClassicClass) about "new" style classes (where you inherit from `object`) and "old" style classes (which doesn't inherit from `object`): "A "New Class" is the recommended way to create a class in modern Python." The word "modern" is meaning Python version 2.2 and up to 2.7, in Python 3 all classes are new-style classes. – Some programmer dude Jun 12 '17 at 11:22

1 Answers1

1

Since Python 3.x, all classes extend object implicitly.

But this was not applicable in Python 2.x. Take a look at New-style and classic classes. In older Python version, you have to explicitly extend the object class

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126