Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e348d1a

Browse files
committed
document changes to metaclasses
1 parent 08a8f5f commit e348d1a

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

Doc/reference/datamodel.rst

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,10 +1484,11 @@ By default, classes are constructed using :func:`type`. A class definition is
14841484
read into a separate namespace and the value of class name is bound to the
14851485
result of ``type(name, bases, dict)``.
14861486

1487-
When the class definition is read, if *__metaclass__* is defined then the
1488-
callable assigned to it will be called instead of :func:`type`. This allows
1489-
classes or functions to be written which monitor or alter the class creation
1490-
process:
1487+
When the class definition is read, if a callable ``metaclass`` keyword argument
1488+
is passed after the bases in the class definition, the callable given will be
1489+
called instead of :func:`type`. If other keyword arguments are passed, they
1490+
will also be passed to the metaclass. This allows classes or functions to be
1491+
written which monitor or alter the class creation process:
14911492

14921493
* Modifying the class dictionary prior to the class being created.
14931494

@@ -1508,21 +1509,19 @@ You can of course also override other class methods (or add new methods); for
15081509
example defining a custom :meth:`__call__` method in the metaclass allows custom
15091510
behavior when the class is called, e.g. not always creating a new instance.
15101511

1511-
1512-
.. data:: __metaclass__
1513-
1514-
This variable can be any callable accepting arguments for ``name``, ``bases``,
1515-
and ``dict``. Upon class creation, the callable is used instead of the built-in
1516-
:func:`type`.
1512+
If the metaclass has a :meth:`__prepare__` attribute (usually implemented as a
1513+
class or static method), it is called before the class body is evaluated with
1514+
the name of the class and a tuple of its bases for arguments. It should return
1515+
an object that supports the mapping interface that will be used to store the
1516+
namespace of the class. The default is a plain dictionary. This could be used,
1517+
for example, to keep track of the order that class attributes are declared in by
1518+
returning an ordered dictionary.
15171519

15181520
The appropriate metaclass is determined by the following precedence rules:
15191521

1520-
* If ``dict['__metaclass__']`` exists, it is used.
1522+
* If the ``metaclass`` keyword argument is based with the bases, it is used.
15211523

1522-
* Otherwise, if there is at least one base class, its metaclass is used (this
1523-
looks for a *__class__* attribute first and if not found, uses its type).
1524-
1525-
* Otherwise, if a global variable named __metaclass__ exists, it is used.
1524+
* Otherwise, if there is at least one base class, its metaclass is used.
15261525

15271526
* Otherwise, the default metaclass (:class:`type`) is used.
15281527

@@ -1922,8 +1921,7 @@ correctness, implicit special method lookup may also bypass the
19221921
... print "Metaclass getattribute invoked"
19231922
... return type.__getattribute__(*args)
19241923
...
1925-
>>> class C(object):
1926-
... __metaclass__ = Meta
1924+
>>> class C(object, metaclass=Meta):
19271925
... def __len__(self):
19281926
... return 10
19291927
... def __getattribute__(*args):

0 commit comments

Comments
 (0)