We have two types of classes in Python 2.x as all know like Old style classes and new Style classes
class OldStyle:
pass
The type of instances of Oldstyle classes is always instance
class NewStyle(object):
pass
New style classes have an advantage of method descriptors, super, getattribute etc.,
The type of instances of NewStyle classes is the class name itself
when you check type of NewStyle class is type and type of object is also type
In [5]: type(NewStyle)
Out[5]: type
In [6]: type(object)
Out[6]: type
In [7]: type(type)
Out[7]: type
So what is the concept of inheriting new style classes from object, as we have seen above that type(type) is also type and type(object) is also type
Why can't we inherit new style classes directly from type?
Can we assume below points to differentiate between object and type?
- If we inherit/create a class from
typeas below, we will certainly end up in creating a metaclass? (Everything is an object in python, including classes, are objects of some other class which istype)
class NewStyle(type):
pass
When Python interpreter sees the above code, it would create an object/instance of type class(class object) with a name NewStyle(which is not a normal instance but class object)
- If we inherit/create a class from
object, it will create a normal instance/object of the class
class NewStyle(object):
pass
isinstance(NewStyle, type)
obj = NewStyle()
print isinstance(obj, NewStyle) # True
print isinstance(NewStyle, type) #True
print isinstance(NewStyle, object) #True
print isinstance(object, type) # True
print isinstance(type, object) # True
So finally we use type to create classes, but we use object to create instances?