Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
e348d1a4
Commit
e348d1a4
authored
Oct 19, 2008
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
document changes to metaclasses
parent
08a8f5ff
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
17 deletions
+15
-17
Doc/reference/datamodel.rst
Doc/reference/datamodel.rst
+15
-17
No files found.
Doc/reference/datamodel.rst
View file @
e348d1a4
...
@@ -1484,10 +1484,11 @@ By default, classes are constructed using :func:`type`. A class definition is
...
@@ -1484,10 +1484,11 @@ By default, classes are constructed using :func:`type`. A class definition is
read into a separate namespace and the value of class name is bound to the
read into a separate namespace and the value of class name is bound to the
result of ``type(name, bases, dict)``.
result of ``type(name, bases, dict)``.
When the class definition is read, if *__metaclass__* is defined then the
When the class definition is read, if a callable ``metaclass`` keyword argument
callable assigned to it will be called instead of :func:`type`. This allows
is passed after the bases in the class definition, the callable given will be
classes or functions to be written which monitor or alter the class creation
called instead of :func:`type`. If other keyword arguments are passed, they
process:
will also be passed to the metaclass. This allows classes or functions to be
written which monitor or alter the class creation process:
* Modifying the class dictionary prior to the class being created.
* Modifying the class dictionary prior to the class being created.
...
@@ -1508,21 +1509,19 @@ You can of course also override other class methods (or add new methods); for
...
@@ -1508,21 +1509,19 @@ 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
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.
behavior when the class is called, e.g. not always creating a new instance.
If the metaclass has a :meth:`__prepare__` attribute (usually implemented as a
.. data:: __metaclass__
class or static method), it is called before the class body is evaluated with
the name of the class and a tuple of its bases for arguments. It should return
This variable can be any callable accepting arguments for ``name``, ``bases``,
an object that supports the mapping interface that will be used to store the
and ``dict``. Upon class creation, the callable is used instead of the built-in
namespace of the class. The default is a plain dictionary. This could be used,
:func:`type`.
for example, to keep track of the order that class attributes are declared in by
returning an ordered dictionary.
The appropriate metaclass is determined by the following precedence rules:
The appropriate metaclass is determined by the following precedence rules:
* If
``dict['__metaclass__']`` exist
s, it is used.
* If
the ``metaclass`` keyword argument is based with the base
s, it is used.
* Otherwise, if there is at least one base class, its metaclass is used (this
* Otherwise, if there is at least one base class, its metaclass is used.
looks for a *__class__* attribute first and if not found, uses its type).
* Otherwise, if a global variable named __metaclass__ exists, it is used.
* Otherwise, the default metaclass (:class:`type`) is used.
* Otherwise, the default metaclass (:class:`type`) is used.
...
@@ -1922,8 +1921,7 @@ correctness, implicit special method lookup may also bypass the
...
@@ -1922,8 +1921,7 @@ correctness, implicit special method lookup may also bypass the
... print "Metaclass getattribute invoked"
... print "Metaclass getattribute invoked"
... return type.__getattribute__(*args)
... return type.__getattribute__(*args)
...
...
>>> class C(object):
>>> class C(object, metaclass=Meta):
... __metaclass__ = Meta
... def __len__(self):
... def __len__(self):
... return 10
... return 10
... def __getattribute__(*args):
... def __getattribute__(*args):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment