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.