'What is the difference between object vs type as base classes?
I would usually initialize a class this way,
class C:
pass
but if I do something like this,
class A(object):
pass
and if I do,
class B(type):
pass
then, if I run,
dir(A)
it gives me this list,
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
'__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', '__weakref__']
while
dir(B)
gives me this list,
['__abstractmethods__', '__base__', '__bases__', '__basicsize__', '__call__',
'__class__', '__delattr__', '__dict__', '__dictoffset__', '__dir__', '__doc__',
'__eq__', '__flags__', '__format__', '__ge__', '__getattribute__', '__gt__',
'__hash__', '__init__', '__init_subclass__', '__instancecheck__',
'__itemsize__', '__le__', '__lt__', '__module__', '__mro__', '__name__',
'__ne__', '__new__', '__prepare__', '__qualname__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasscheck__', '__subclasses__', '__subclasshook__',
'__text_signature__', '__weakrefoffset__', 'mro']
but I can use these on class A also, for example, I can use A.__bases__, A.mro, although it is not listed in dir(A)
A.mro, A.__bases__
gives,
(<function A.mro>, (object,))
while,
B.mro, B.__bases__
gives,
(<method 'mro' of 'type' objects>, (type,))
Furthermore, when I create instances, then,
# both have object as base class
a = A()
c = C()
works, but,
# type as base class
b = B()
gives this error,
TypeError: type.__new__() takes exactly 3 arguments (0 given)
It appears that by default object is the base class, but I still am confused when exactly should I specify, type as a base class?
And what more differences are there between the two?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
