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?

2
  • 1
    The difference between old style and new style classes exists only in Python 2.x. Commented Jun 12, 2017 at 11:22
  • 1
    From the first sentence of this page 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. Commented Jun 12, 2017 at 11:22

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.