Pravin Pravin -4 years ago 45
Python Question

Different behaviour of class python

>>> 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 other is inherited from object base class.

I kind of read somewhere if you don't inherit the default 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???

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download