βΉοΈ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.3 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://en.wikibooks.org/wiki/Python_Programming/Metaclasses |
| Last Crawled | 2026-04-03 18:52:36 (9 days ago) |
| First Indexed | 2015-06-29 02:13:34 (10 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Python Programming/Metaclasses - Wikibooks, open books for an open world |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | From Wikibooks, open books for an open world
In Python, classes are themselves objects. Just as other objects are instances of a particular class, classes themselves are instances of a metaclass.
The
Pep 3115
defines the changes to python 3 metaclasses.
In python3 you have a method __prepare__ that is called in the metaclass to create a dictionary or other class to store the class members.
[
1
]
Then there is the __new__ method that is called to create new instances of that class.
[
2
]
The metaclass for all standard Python types is the "type" object.
>>>
type
(
object
)
<
type
'type'
>
>>>
type
(
int
)
<
type
'type'
>
>>>
type
(
list
)
<
type
'type'
>
Just like list, int and object, "type" is itself a normal Python object, and is itself an instance of a class. In this case, it is in fact an instance of itself.
>>>
type
(
type
)
<
type
'type'
>
It can be instantiated to create new class objects similarly to the class factory example above by passing the name of the new class, the base classes to inherit from, and a dictionary defining the namespace to use.
For instance, the code:
>>>
class
MyClass
(
BaseClass
):
...
attribute
=
42
Could also be written as:
>>>
MyClass
=
type
(
"MyClass"
,
(
BaseClass
,),
{
'attribute'
:
42
})
It is possible to create a class with a different metaclass than type by setting the metaclass keyword argument when defining the class. When this is done, the class, and its subclass will be created using your custom metaclass. For example
class
CustomMetaclass
(
type
):
def
__init__
(
cls
,
name
,
bases
,
dct
):
print
(
"Creating class
%s
using CustomMetaclass"
%
name
)
super
(
CustomMetaclass
,
cls
)
.
__init__
(
name
,
bases
,
dct
)
class
BaseClass
(
metaclass
=
CustomMetaclass
):
pass
class
Subclass1
(
BaseClass
):
pass
This will print
Creating class BaseClass using CustomMetaclass
Creating class Subclass1 using CustomMetaclass
By creating a custom metaclass in this way, it is possible to change how the class is constructed. This allows you to add or remove attributes and methods, register creation of classes and subclasses creation and various other manipulations when the class is created.
Wikipedia article on Aspect Oriented Programming
Unifying types and classes in Python 2.2
O'Reilly Article on Python Metaclasses
β
http://www.python.org/dev/peps/pep-3115/
β
http://eli.thegreenplace.net/2011/08/14/python-metaclasses-by-example/
To do:
[Incomplete] (see Putting Metaclasses to Work, Ira R. Forman, Scott H. Danforth?) |
| Markdown | [Jump to content](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses#bodyContent)
Main menu
Main menu
move to sidebar
hide
Navigation
- [Main Page](https://en.wikibooks.org/wiki/Main_Page "Visit the main page [alt-z]")
- [Help](https://en.wikibooks.org/wiki/Help:Contents "Find help on how to use and edit Wikibooks")
- [Browse](https://en.wikibooks.org/wiki/Wikibooks:Card_Catalog_Office "Check out what Wikibooks has to offer")
- [Cookbook](https://en.wikibooks.org/wiki/Cookbook:Table_of_Contents "Learn recipes from around the world")
- [Wikijunior](https://en.wikibooks.org/wiki/Wikijunior "Books for children")
- [Featured books](https://en.wikibooks.org/wiki/Wikibooks:Featured_books "The best of Wikibooks")
- [Recent changes](https://en.wikibooks.org/wiki/Special:RecentChanges "A list of recent changes in the wiki [alt-r]")
- [Special pages](https://en.wikibooks.org/wiki/Special:SpecialPages "A list of all special pages [alt-q]")
- [Random book](https://en.wikibooks.org/wiki/Special:RandomInCategory/Book:Wikibooks_Stacks/Books)
- [Using Wikibooks](https://en.wikibooks.org/wiki/Using_Wikibooks)
Community
- [Reading room forum](https://en.wikibooks.org/wiki/Wikibooks:Reading_room)
- [Community portal](https://en.wikibooks.org/wiki/Wikibooks:Community_Portal "Find your way around the Wikibooks community")
- [Help out\!](https://en.wikibooks.org/wiki/Wikibooks:Maintenance "Frequent tasks that you can help with")
- [Policies and guidelines](https://en.wikibooks.org/wiki/Wikibooks:Policies_and_guidelines "Pages detailing important rules and procedures")
- [Contact us](https://en.wikibooks.org/wiki/Wikibooks:Contact_us "Alternative methods of communication")
[  ](https://en.wikibooks.org/wiki/Main_Page)
[Search](https://en.wikibooks.org/wiki/Special:Search "Search Wikibooks [alt-f]")
Appearance
Appearance
move to sidebar
hide
Text
This page always uses small font size
Width
The content is as wide as possible for your browser window.
Color (beta)
This page is always in light mode.
- [Donations](https://donate.wikimedia.org/?wmf_source=donate&wmf_medium=sidebar&wmf_campaign=en.wikibooks.org&uselang=en)
- [Create account](https://en.wikibooks.org/w/index.php?title=Special:CreateAccount&returnto=Python+Programming%2FMetaclasses "You are encouraged to create an account and log in; however, it is not mandatory")
- [Log in](https://en.wikibooks.org/w/index.php?title=Special:UserLogin&returnto=Python+Programming%2FMetaclasses "You are encouraged to log in; however, it is not mandatory [alt-o]")
Personal tools
- [Donations](https://donate.wikimedia.org/?wmf_source=donate&wmf_medium=sidebar&wmf_campaign=en.wikibooks.org&uselang=en)
- [Create account](https://en.wikibooks.org/w/index.php?title=Special:CreateAccount&returnto=Python+Programming%2FMetaclasses "You are encouraged to create an account and log in; however, it is not mandatory")
- [Log in](https://en.wikibooks.org/w/index.php?title=Special:UserLogin&returnto=Python+Programming%2FMetaclasses "You are encouraged to log in; however, it is not mandatory [alt-o]")
Toggle the table of contents
## Contents
move to sidebar
hide
- [Beginning](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses)
- [1 Python3](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses#Python3)
- [2 The type Metaclass](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses#The_type_Metaclass)
- [3 Metaclasses](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses#Metaclasses)
- [4 More resources](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses#More_resources)
- [5 References](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses#References)
# Python Programming/Metaclasses
1 language
- [δΈζ](https://zh.wikibooks.org/wiki/Python/%E5%85%83%E7%B1%BB "Python/ε
η±» β Chinese")
[Edit links](https://www.wikidata.org/wiki/Special:EntityPage/Q96920717#sitelinks-wikibooks "Edit interlanguage links")
- [Book](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses "View the content page [alt-c]")
- [Discussion](https://en.wikibooks.org/wiki/Talk:Python_Programming/Metaclasses "Discussion about the content page [alt-t]")
English
- [Read](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses)
- [Edit](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&veaction=edit "Edit this page [alt-v]")
- [Edit source](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&action=edit "Edit the source code of this page [alt-e]")
- [View history](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&action=history "Past revisions of this page [alt-h]")
Tools
Tools
move to sidebar
hide
Actions
- [Read](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses)
- [Edit](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&veaction=edit "Edit this page [alt-v]")
- [Edit source](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&action=edit "Edit the source code of this page [alt-e]")
- [View history](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&action=history)
General
- [What links here](https://en.wikibooks.org/wiki/Special:WhatLinksHere/Python_Programming/Metaclasses "A list of all wiki pages that link here [alt-j]")
- [Related changes](https://en.wikibooks.org/wiki/Special:RecentChangesLinked/Python_Programming/Metaclasses "Recent changes in pages linked from this page [alt-k]")
- [Upload file](https://commons.wikimedia.org/wiki/Special:UploadWizard?uselang=en "Upload files [alt-u]")
- [Permanent link](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&oldid=4070786 "Permanent link to this revision of this page")
- [Page information](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&action=info "More information about this page")
- [Cite this page](https://en.wikibooks.org/w/index.php?title=Special:CiteThisPage&page=Python_Programming%2FMetaclasses&id=4070786&wpFormIdentifier=titleform "Information on how to cite this page")
- [Get shortened URL](https://en.wikibooks.org/w/index.php?title=Special:UrlShortener&url=https%3A%2F%2Fen.wikibooks.org%2Fwiki%2FPython_Programming%2FMetaclasses)
- [Edit interlanguage links](https://www.wikidata.org/wiki/Special:EntityPage/Q96920717#sitelinks-wikibooks "Edit interlanguage links")
Sister projects
- [Wikipedia](https://en.wikipedia.org/wiki/Main_Page)
- [Wikiversity](https://en.wikiversity.org/wiki/Wikiversity:Main_Page)
- [Wiktionary](https://en.wiktionary.org/wiki/Wiktionary:Main_Page)
- [Wikiquote](https://en.wikiquote.org/wiki/Main_Page)
- [Wikisource](https://en.wikisource.org/wiki/Main_Page)
- [Wikinews](https://en.wikinews.org/wiki/Main_Page)
- [Wikivoyage](https://en.wikivoyage.org/wiki/Main_Page)
- [Commons](https://commons.wikimedia.org/wiki/Main_Page)
- [Wikidata](https://www.wikidata.org/wiki/Wikidata:Main_Page)
- [MediaWiki](https://www.mediawiki.org/wiki/Main_Page)
- [Meta-Wiki](https://meta.wikimedia.org/wiki/Main_Page)
Print/export
- [Create a collection](https://en.wikibooks.org/w/index.php?title=Special:Book&bookcmd=book_creator&referer=Python+Programming%2FMetaclasses)
- [Download as PDF](https://en.wikibooks.org/w/index.php?title=Special:DownloadAsPdf&page=Python_Programming%2FMetaclasses&action=show-download-screen)
- [Printable version](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&printable=yes "Printable version of this page [alt-p]")
In other projects
- [Wikidata item](https://www.wikidata.org/wiki/Special:EntityPage/Q96920717 "Link to connected data repository item [alt-g]")
From Wikibooks, open books for an open world
\<
[Python Programming](https://en.wikibooks.org/wiki/Python_Programming "Python Programming")
| | | |
|---|---|---|
| [Previous: Reflection](https://en.wikibooks.org/wiki/Python_Programming/Reflection "Python Programming/Reflection") | [Index](https://en.wikibooks.org/wiki/Python_Programming "Python Programming") | [Next: Tips and Tricks](https://en.wikibooks.org/wiki/Python_Programming/Tips_and_Tricks "Python Programming/Tips and Tricks") |
In Python, classes are themselves objects. Just as other objects are instances of a particular class, classes themselves are instances of a metaclass.
### Python3
\[[edit](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&veaction=edit§ion=1 "Edit section: Python3") \| [edit source](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&action=edit§ion=1 "Edit section's source code: Python3")\]
The [Pep 3115](https://www.python.org/dev/peps/pep-3115/) defines the changes to python 3 metaclasses. In python3 you have a method \_\_prepare\_\_ that is called in the metaclass to create a dictionary or other class to store the class members.[\[1\]](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses#cite_note-1) Then there is the \_\_new\_\_ method that is called to create new instances of that class. [\[2\]](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses#cite_note-2)
### The type Metaclass
\[[edit](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&veaction=edit§ion=2 "Edit section: The type Metaclass") \| [edit source](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&action=edit§ion=2 "Edit section's source code: The type Metaclass")\]
The metaclass for all standard Python types is the "type" object.
```
>>> type(object)
<type 'type'>
>>> type(int)
<type 'type'>
>>> type(list)
<type 'type'>
```
Just like list, int and object, "type" is itself a normal Python object, and is itself an instance of a class. In this case, it is in fact an instance of itself.
```
>>> type(type)
<type 'type'>
```
It can be instantiated to create new class objects similarly to the class factory example above by passing the name of the new class, the base classes to inherit from, and a dictionary defining the namespace to use.
For instance, the code:
```
>>> class MyClass(BaseClass):
... attribute = 42
```
Could also be written as:
```
>>> MyClass = type("MyClass", (BaseClass,), {'attribute' : 42})
```
### Metaclasses
\[[edit](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&veaction=edit§ion=3 "Edit section: Metaclasses") \| [edit source](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&action=edit§ion=3 "Edit section's source code: Metaclasses")\]
It is possible to create a class with a different metaclass than type by setting the metaclass keyword argument when defining the class. When this is done, the class, and its subclass will be created using your custom metaclass. For example
```
class CustomMetaclass(type):
def __init__(cls, name, bases, dct):
print("Creating class %s using CustomMetaclass" % name)
super(CustomMetaclass, cls).__init__(name, bases, dct)
class BaseClass(metaclass=CustomMetaclass):
pass
class Subclass1(BaseClass):
pass
```
This will print
```
Creating class BaseClass using CustomMetaclass
Creating class Subclass1 using CustomMetaclass
```
By creating a custom metaclass in this way, it is possible to change how the class is constructed. This allows you to add or remove attributes and methods, register creation of classes and subclasses creation and various other manipulations when the class is created.
### More resources
\[[edit](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&veaction=edit§ion=4 "Edit section: More resources") \| [edit source](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&action=edit§ion=4 "Edit section's source code: More resources")\]
- [Wikipedia article on Aspect Oriented Programming](https://en.wikipedia.org/wiki/Aspect-oriented_programming "w:Aspect-oriented programming")
- [Unifying types and classes in Python 2.2](https://www.python.org/2.2/descrintro.html)
- [O'Reilly Article on Python Metaclasses](http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html)
### References
\[[edit](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&veaction=edit§ion=5 "Edit section: References") \| [edit source](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&action=edit§ion=5 "Edit section's source code: References")\]
1. [β](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses#cite_ref-1 "Jump up") [http://www.python.org/dev/peps/pep-3115/](https://www.python.org/dev/peps/pep-3115/)
2. [β](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses#cite_ref-2 "Jump up") <http://eli.thegreenplace.net/2011/08/14/python-metaclasses-by-example/>
| | |
|---|---|
|  | **To do:** \[Incomplete\] (see Putting Metaclasses to Work, Ira R. Forman, Scott H. Danforth?) |
| | | |
|---|---|---|
| [Previous: Reflection](https://en.wikibooks.org/wiki/Python_Programming/Reflection "Python Programming/Reflection") | [Index](https://en.wikibooks.org/wiki/Python_Programming "Python Programming") | [Next: Tips and Tricks](https://en.wikibooks.org/wiki/Python_Programming/Tips_and_Tricks "Python Programming/Tips and Tricks") |

Retrieved from "<https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&oldid=4070786>"
[Category](https://en.wikibooks.org/wiki/Special:Categories "Special:Categories"):
- [Book:Python Programming](https://en.wikibooks.org/wiki/Category:Book:Python_Programming "Category:Book:Python Programming")
Hidden category:
- [Wikibooks pages with to-do lists](https://en.wikibooks.org/wiki/Category:Wikibooks_pages_with_to-do_lists "Category:Wikibooks pages with to-do lists")
- This page was last edited on 5 June 2022, at 08:10.
- Text is available under the [Creative Commons Attribution-ShareAlike License](https://creativecommons.org/licenses/by-sa/4.0/); additional terms may apply. By using this site, you agree to the [Terms of Use](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Terms_of_Use) and [Privacy Policy.](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Privacy_policy)
- [Privacy policy](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Privacy_policy)
- [About Wikibooks](https://en.wikibooks.org/wiki/Wikibooks:Welcome)
- [Disclaimers](https://en.wikibooks.org/wiki/Wikibooks:General_disclaimer)
- [Code of Conduct](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Universal_Code_of_Conduct)
- [Developers](https://developer.wikimedia.org/)
- [Statistics](https://stats.wikimedia.org/#/en.wikibooks.org)
- [Cookie statement](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Cookie_statement)
- [Mobile view](https://en.wikibooks.org/w/index.php?title=Python_Programming/Metaclasses&mobileaction=toggle_view_mobile)
- [](https://www.wikimedia.org/)
- [](https://www.mediawiki.org/)
Search
Toggle the table of contents
Python Programming/Metaclasses
1 language
[Add topic](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses) |
| Readable Markdown | From Wikibooks, open books for an open world
In Python, classes are themselves objects. Just as other objects are instances of a particular class, classes themselves are instances of a metaclass.
The [Pep 3115](https://www.python.org/dev/peps/pep-3115/) defines the changes to python 3 metaclasses. In python3 you have a method \_\_prepare\_\_ that is called in the metaclass to create a dictionary or other class to store the class members.[\[1\]](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses#cite_note-1) Then there is the \_\_new\_\_ method that is called to create new instances of that class. [\[2\]](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses#cite_note-2)
The metaclass for all standard Python types is the "type" object.
```
>>> type(object)
<type 'type'>
>>> type(int)
<type 'type'>
>>> type(list)
<type 'type'>
```
Just like list, int and object, "type" is itself a normal Python object, and is itself an instance of a class. In this case, it is in fact an instance of itself.
```
>>> type(type)
<type 'type'>
```
It can be instantiated to create new class objects similarly to the class factory example above by passing the name of the new class, the base classes to inherit from, and a dictionary defining the namespace to use.
For instance, the code:
```
>>> class MyClass(BaseClass):
... attribute = 42
```
Could also be written as:
```
>>> MyClass = type("MyClass", (BaseClass,), {'attribute' : 42})
```
It is possible to create a class with a different metaclass than type by setting the metaclass keyword argument when defining the class. When this is done, the class, and its subclass will be created using your custom metaclass. For example
```
class CustomMetaclass(type):
def __init__(cls, name, bases, dct):
print("Creating class %s using CustomMetaclass" % name)
super(CustomMetaclass, cls).__init__(name, bases, dct)
class BaseClass(metaclass=CustomMetaclass):
pass
class Subclass1(BaseClass):
pass
```
This will print
```
Creating class BaseClass using CustomMetaclass
Creating class Subclass1 using CustomMetaclass
```
By creating a custom metaclass in this way, it is possible to change how the class is constructed. This allows you to add or remove attributes and methods, register creation of classes and subclasses creation and various other manipulations when the class is created.
- [Wikipedia article on Aspect Oriented Programming](https://en.wikipedia.org/wiki/Aspect-oriented_programming "w:Aspect-oriented programming")
- [Unifying types and classes in Python 2.2](https://www.python.org/2.2/descrintro.html)
- [O'Reilly Article on Python Metaclasses](http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html)
1. [β](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses#cite_ref-1 "Jump up") [http://www.python.org/dev/peps/pep-3115/](https://www.python.org/dev/peps/pep-3115/)
2. [β](https://en.wikibooks.org/wiki/Python_Programming/Metaclasses#cite_ref-2 "Jump up") <http://eli.thegreenplace.net/2011/08/14/python-metaclasses-by-example/>
| | |
|---|---|
|  | **To do:** \[Incomplete\] (see Putting Metaclasses to Work, Ira R. Forman, Scott H. Danforth?) | |
| Shard | 102 (laksa) |
| Root Hash | 7794694764302024702 |
| Unparsed URL | org,wikibooks!en,/wiki/Python_Programming/Metaclasses s443 |