[Python-checkins] r59837 - python/trunk/Doc/reference/datamodel.rst
georg.brandl
python-checkins at python.org
Mon Jan 7 20:17:10 CET 2008
Author: georg.brandl
Date: Mon Jan 7 20:17:10 2008
New Revision: 59837
Modified:
python/trunk/Doc/reference/datamodel.rst
Log:
Clarify metaclass docs and add example.
Modified: python/trunk/Doc/reference/datamodel.rst
==============================================================================
--- python/trunk/Doc/reference/datamodel.rst (original)
+++ python/trunk/Doc/reference/datamodel.rst Mon Jan 7 20:17:10 2008
@@ -1177,7 +1177,8 @@
:meth:`__init__` method will not be invoked.
:meth:`__new__` is intended mainly to allow subclasses of immutable types (like
- int, str, or tuple) to customize instance creation.
+ int, str, or tuple) to customize instance creation. It is also commonly
+ overridden in custom metaclasses in order to customize class creation.
.. method:: object.__init__(self[, ...])
@@ -1651,7 +1652,7 @@
bound to the result of ``type(name, bases, dict)``.
When the class definition is read, if *__metaclass__* is defined then the
-callable assigned to it will be called instead of :func:`type`. The allows
+callable assigned to it will be called instead of :func:`type`. This allows
classes or functions to be written which monitor or alter the class creation
process:
@@ -1660,6 +1661,20 @@
* Returning an instance of another class -- essentially performing the role of a
factory function.
+These steps will have to be performed in the metaclass's :meth:`__new__` method
+-- :meth:`type.__new__` can then be called from this method to create a class
+with different properties. This example adds a new element to the class
+dictionary before creating the class::
+
+ class metacls(type):
+ def __new__(mcs, name, bases, dict):
+ dict['foo'] = 'metacls was here'
+ return type.__new__(mcs, name, bases, dict)
+
+You can of course also override other class methods (or add new methods); for
+example defining a custom :meth:`__call__` method in the metaclass allows custom
+behavior when the class is called, e.g. not always creating a new instance.
+
.. data:: __metaclass__
More information about the Python-checkins
mailing list