Docs:
With three arguments, return a new type object. This is essentially a dynamic form of the class statement. The name string is the class name and becomes the
__name__
attribute; the bases tuple itemizes the base classes and becomes the__bases__
attribute; and the dict dictionary is the namespace containing definitions for class body and becomes the__dict__
attribute.
While learning Python I have come across this use of type as "a dynamic form of the class statement", this seems very interesting and useful and I wish to understand it better. Can someone clearly explain the role of __name__
, __bases__
, and __dict__
in a class and also give example where type(name, bases, dict)
comes into its own right.
When you define a class:
class Foo(Base1, Base2):
bar = 'baz'
def spam(self):
return 'ham'
Python essentially does this:
def spam(self):
return 'ham'
body = {'bar': 'baz', 'spam': spam}
Foo = type('Foo', (Base1, Base2), body)
where Foo
is then added to the namespace the class
statement was defined in.
The block under the class
statement is executed as if it was a function with no arguments and the resulting local namespace (a dictionary) forms the class body; in this case a dictionary with 'bar'
and 'spam'
keys is formed.
You can achieve the same result by passing a name string, a tuple of base classes and a dictionary with string keys to the type()
function.
Once you start exploring metaclasses as well (which are essentially subclasses of type()
, letting you customize how classes are created), you'll find that body
doesn't have to be a plain dictionary. PEP 3115 - Metaclasses in Python 3 expanded the possible values to anything dict like, letting you implement all sorts of interesting class behaviour by letting you use augmented dictionaries for the class body. The new Python 3.4 enum
module for example, uses an OrderedDict()
instance instead, to preserve attribute ordering.