| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="ref.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="ref.html" title='Python Reference Manual' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="callable-types.html" /> |
| 13 | <link rel="prev" href="attribute-access.html" /> |
| 14 | <link rel="parent" href="specialnames.html" /> |
| 15 | <link rel="next" href="callable-types.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>3.3.3 Customizing class creation</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="3.3.2.4 __slots__" |
| 25 | href="slots.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="3.3 Special method names" |
| 28 | href="specialnames.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="3.3.4 Emulating callable objects" |
| 31 | href="callable-types.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Reference Manual</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="slots.html">3.3.2.4 __slots__</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="specialnames.html">3.3 Special method names</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="callable-types.html">3.3.4 Emulating callable objects</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION005330000000000000000"></A><A NAME="metaclasses"></A> |
| 56 | <BR> |
| 57 | 3.3.3 Customizing class creation |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | By default, new-style classes are constructed using <tt class="function">type()</tt>. |
| 62 | A class definition is read into a separate namespace and the value |
| 63 | of class name is bound to the result of <code>type(name, bases, dict)</code>. |
| 64 | |
| 65 | <P> |
| 66 | When the class definition is read, if <var>__metaclass__</var> is defined |
| 67 | then the callable assigned to it will be called instead of <tt class="function">type()</tt>. |
| 68 | The allows classes or functions to be written which monitor or alter the class |
| 69 | creation process: |
| 70 | |
| 71 | <P> |
| 72 | |
| 73 | <UL> |
| 74 | <LI>Modifying the class dictionary prior to the class being created. |
| 75 | </LI> |
| 76 | <LI>Returning an instance of another class - essentially performing |
| 77 | the role of a factory function. |
| 78 | </LI> |
| 79 | </UL> |
| 80 | |
| 81 | <P> |
| 82 | <dl><dt><b><tt id='l2h-218' xml:id='l2h-218'>__metaclass__</tt></b></dt> |
| 83 | <dd> |
| 84 | This variable can be any callable accepting arguments for <code>name</code>, |
| 85 | <code>bases</code>, and <code>dict</code>. Upon class creation, the callable is |
| 86 | used instead of the built-in <tt class="function">type()</tt>. |
| 87 | |
| 88 | <span class="versionnote">New in version 2.2.</span> |
| 89 | |
| 90 | </dd></dl> |
| 91 | |
| 92 | <P> |
| 93 | The appropriate metaclass is determined by the following precedence rules: |
| 94 | |
| 95 | <P> |
| 96 | |
| 97 | <UL> |
| 98 | <LI>If <code>dict['__metaclass__']</code> exists, it is used. |
| 99 | |
| 100 | <P> |
| 101 | </LI> |
| 102 | <LI>Otherwise, if there is at least one base class, its metaclass is used |
| 103 | (this looks for a <var>__class__</var> attribute first and if not found, uses its |
| 104 | type). |
| 105 | |
| 106 | <P> |
| 107 | </LI> |
| 108 | <LI>Otherwise, if a global variable named __metaclass__ exists, it is used. |
| 109 | |
| 110 | <P> |
| 111 | </LI> |
| 112 | <LI>Otherwise, the old-style, classic metaclass (types.ClassType) is used. |
| 113 | |
| 114 | <P> |
| 115 | </LI> |
| 116 | </UL> |
| 117 | |
| 118 | <P> |
| 119 | The potential uses for metaclasses are boundless. Some ideas that have |
| 120 | been explored including logging, interface checking, automatic delegation, |
| 121 | automatic property creation, proxies, frameworks, and automatic resource |
| 122 | locking/synchronization. |
| 123 | |
| 124 | <P> |
| 125 | |
| 126 | <DIV CLASS="navigation"> |
| 127 | <div class='online-navigation'> |
| 128 | <p></p><hr /> |
| 129 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 130 | <tr> |
| 131 | <td class='online-navigation'><a rel="prev" title="3.3.2.4 __slots__" |
| 132 | href="slots.html"><img src='../icons/previous.png' |
| 133 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 134 | <td class='online-navigation'><a rel="parent" title="3.3 Special method names" |
| 135 | href="specialnames.html"><img src='../icons/up.png' |
| 136 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 137 | <td class='online-navigation'><a rel="next" title="3.3.4 Emulating callable objects" |
| 138 | href="callable-types.html"><img src='../icons/next.png' |
| 139 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 140 | <td align="center" width="100%">Python Reference Manual</td> |
| 141 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 142 | href="contents.html"><img src='../icons/contents.png' |
| 143 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 144 | <td class='online-navigation'><img src='../icons/blank.png' |
| 145 | border='0' height='32' alt='' width='32' /></td> |
| 146 | <td class='online-navigation'><a rel="index" title="Index" |
| 147 | href="genindex.html"><img src='../icons/index.png' |
| 148 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 149 | </tr></table> |
| 150 | <div class='online-navigation'> |
| 151 | <b class="navlabel">Previous:</b> |
| 152 | <a class="sectref" rel="prev" href="slots.html">3.3.2.4 __slots__</A> |
| 153 | <b class="navlabel">Up:</b> |
| 154 | <a class="sectref" rel="parent" href="specialnames.html">3.3 Special method names</A> |
| 155 | <b class="navlabel">Next:</b> |
| 156 | <a class="sectref" rel="next" href="callable-types.html">3.3.4 Emulating callable objects</A> |
| 157 | </div> |
| 158 | </div> |
| 159 | <hr /> |
| 160 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 161 | </DIV> |
| 162 | <!--End of Navigation Panel--> |
| 163 | <ADDRESS> |
| 164 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 165 | </ADDRESS> |
| 166 | </BODY> |
| 167 | </HTML> |