| 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="prev" href="function.html" /> |
| 13 | <link rel="parent" href="compound.html" /> |
| 14 | <link rel="next" href="top-level.html" /> |
| 15 | <meta name='aesop' content='information' /> |
| 16 | <title>7.6 Class definitions</title> |
| 17 | </head> |
| 18 | <body> |
| 19 | <DIV CLASS="navigation"> |
| 20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 22 | <tr> |
| 23 | <td class='online-navigation'><a rel="prev" title="7.5 Function definitions" |
| 24 | href="function.html"><img src='../icons/previous.png' |
| 25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 26 | <td class='online-navigation'><a rel="parent" title="7. Compound statements" |
| 27 | href="compound.html"><img src='../icons/up.png' |
| 28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 29 | <td class='online-navigation'><a rel="next" title="8. Top-level components" |
| 30 | href="top-level.html"><img src='../icons/next.png' |
| 31 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 32 | <td align="center" width="100%">Python Reference Manual</td> |
| 33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 34 | href="contents.html"><img src='../icons/contents.png' |
| 35 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 36 | <td class='online-navigation'><img src='../icons/blank.png' |
| 37 | border='0' height='32' alt='' width='32' /></td> |
| 38 | <td class='online-navigation'><a rel="index" title="Index" |
| 39 | href="genindex.html"><img src='../icons/index.png' |
| 40 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 41 | </tr></table> |
| 42 | <div class='online-navigation'> |
| 43 | <b class="navlabel">Previous:</b> |
| 44 | <a class="sectref" rel="prev" href="function.html">7.5 Function definitions</A> |
| 45 | <b class="navlabel">Up:</b> |
| 46 | <a class="sectref" rel="parent" href="compound.html">7. Compound statements</A> |
| 47 | <b class="navlabel">Next:</b> |
| 48 | <a class="sectref" rel="next" href="top-level.html">8. Top-level components</A> |
| 49 | </div> |
| 50 | <hr /></div> |
| 51 | </DIV> |
| 52 | <!--End of Navigation Panel--> |
| 53 | |
| 54 | <H1><A NAME="SECTION009600000000000000000"></A><A NAME="class"></A> |
| 55 | <BR> |
| 56 | 7.6 Class definitions |
| 57 | </H1> |
| 58 | <a id='l2h-614' xml:id='l2h-614'></a><a id='l2h-615' xml:id='l2h-615'></a> |
| 59 | <P> |
| 60 | A class definition defines a class object (see section <A href="types.html#types">3.2</A>): |
| 61 | <a id='l2h-616' xml:id='l2h-616'></a> |
| 62 | <P> |
| 63 | <dl><dd class="grammar"> |
| 64 | <div class="productions"> |
| 65 | <table> |
| 66 | <tr> |
| 67 | <td><a id='tok-classdef' xml:id='tok-classdef'>classdef</a></td> |
| 68 | <td>::=</td> |
| 69 | <td>"class" <a class='grammartoken' href="class.html#tok-classname">classname</a> [<a class='grammartoken' href="class.html#tok-inheritance">inheritance</a>] ":" |
| 70 | <a class='grammartoken' href="compound.html#tok-suite">suite</a></td></tr> |
| 71 | <tr> |
| 72 | <td><a id='tok-inheritance' xml:id='tok-inheritance'>inheritance</a></td> |
| 73 | <td>::=</td> |
| 74 | <td>"(" <a class='grammartoken' href="exprlists.html#tok-expression_list">expression_list</a> ")"</td></tr> |
| 75 | <tr> |
| 76 | <td><a id='tok-classname' xml:id='tok-classname'>classname</a></td> |
| 77 | <td>::=</td> |
| 78 | <td><a class='grammartoken' href="identifiers.html#tok-identifier">identifier</a></td></tr> |
| 79 | </table> |
| 80 | </div> |
| 81 | <a class="grammar-footer" |
| 82 | href="grammar.txt" type="text/plain" |
| 83 | >Download entire grammar as text.</a> |
| 84 | </dd></dl> |
| 85 | |
| 86 | <P> |
| 87 | A class definition is an executable statement. It first evaluates the |
| 88 | inheritance list, if present. Each item in the inheritance list |
| 89 | should evaluate to a class object or class type which allows |
| 90 | subclassing. The class's suite is then executed |
| 91 | in a new execution frame (see section <A href="naming.html#naming">4.1</A>), using a newly |
| 92 | created local namespace and the original global namespace. |
| 93 | (Usually, the suite contains only function definitions.) When the |
| 94 | class's suite finishes execution, its execution frame is discarded but |
| 95 | its local namespace is saved. A class object is then created using |
| 96 | the inheritance list for the base classes and the saved local |
| 97 | namespace for the attribute dictionary. The class name is bound to this |
| 98 | class object in the original local namespace. |
| 99 | <a id='l2h-620' xml:id='l2h-620'></a> |
| 100 | <a id='l2h-617' xml:id='l2h-617'></a><a id='l2h-618' xml:id='l2h-618'></a><a id='l2h-619' xml:id='l2h-619'></a> |
| 101 | <P> |
| 102 | <strong>Programmer's note:</strong> Variables defined in the class definition |
| 103 | are class variables; they are shared by all instances. To define |
| 104 | instance variables, they must be given a value in the |
| 105 | <tt class="method">__init__()</tt> method or in another method. Both class and |
| 106 | instance variables are accessible through the notation |
| 107 | ``<code>self.name</code>'', and an instance variable hides a class variable |
| 108 | with the same name when accessed in this way. Class variables with |
| 109 | immutable values can be used as defaults for instance variables. |
| 110 | For new-style classes, descriptors can be used to create instance |
| 111 | variables with different implementation details. |
| 112 | |
| 113 | <DIV CLASS="navigation"> |
| 114 | <div class='online-navigation'> |
| 115 | <p></p><hr /> |
| 116 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 117 | <tr> |
| 118 | <td class='online-navigation'><a rel="prev" title="7.5 Function definitions" |
| 119 | href="function.html"><img src='../icons/previous.png' |
| 120 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 121 | <td class='online-navigation'><a rel="parent" title="7. Compound statements" |
| 122 | href="compound.html"><img src='../icons/up.png' |
| 123 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 124 | <td class='online-navigation'><a rel="next" title="8. Top-level components" |
| 125 | href="top-level.html"><img src='../icons/next.png' |
| 126 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 127 | <td align="center" width="100%">Python Reference Manual</td> |
| 128 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 129 | href="contents.html"><img src='../icons/contents.png' |
| 130 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 131 | <td class='online-navigation'><img src='../icons/blank.png' |
| 132 | border='0' height='32' alt='' width='32' /></td> |
| 133 | <td class='online-navigation'><a rel="index" title="Index" |
| 134 | href="genindex.html"><img src='../icons/index.png' |
| 135 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 136 | </tr></table> |
| 137 | <div class='online-navigation'> |
| 138 | <b class="navlabel">Previous:</b> |
| 139 | <a class="sectref" rel="prev" href="function.html">7.5 Function definitions</A> |
| 140 | <b class="navlabel">Up:</b> |
| 141 | <a class="sectref" rel="parent" href="compound.html">7. Compound statements</A> |
| 142 | <b class="navlabel">Next:</b> |
| 143 | <a class="sectref" rel="next" href="top-level.html">8. Top-level components</A> |
| 144 | </div> |
| 145 | </div> |
| 146 | <hr /> |
| 147 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 148 | </DIV> |
| 149 | <!--End of Navigation Panel--> |
| 150 | <ADDRESS> |
| 151 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 152 | </ADDRESS> |
| 153 | </BODY> |
| 154 | </HTML> |