Reputation: 3723
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?
Upvotes: 2
Views: 83
Reputation: 3723
it appears that as soon as I specify type
as the base class, my class becomes a class creator.
so, instances of a class with type
as the base class, will be classes.
that is,
# with type as base class
b = B()
would give an error,
TypeError: type.__new__() takes exactly 3 arguments (0 given)
but,
b = B('B', (object,), {})
would work, and now, b
itself is a class, and I can create instances of it
d = b()
type(d)
gives,
__main__.B
whereas, if I specified,
b = B('B', (type,), {})
then b
is again a class creator, and instances of it will be classes themselves.
Upvotes: 1