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
3ccb49af
Commit
3ccb49af
authored
Jan 07, 2008
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clarify metaclass docs and add example.
parent
61d28864
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
2 deletions
+17
-2
Doc/reference/datamodel.rst
Doc/reference/datamodel.rst
+17
-2
No files found.
Doc/reference/datamodel.rst
View file @
3ccb49af
...
...
@@ -1177,7 +1177,8 @@ Basic customization
: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 @@ definition is read into a separate namespace and the value of class name is
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`. Th
e
allows
callable assigned to it will be called instead of :func:`type`. Th
is
allows
classes or functions to be written which monitor or alter the class creation
process:
...
...
@@ -1660,6 +1661,20 @@ process:
* 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__
...
...
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