You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
8.1 KiB
94 lines
8.1 KiB
<div class="section" id="class-definitions">
|
|
<span id="class"></span><h2>Class definitions</h2>
|
|
<p id="index-26">A class definition defines a class object (see section <a class="reference internal" href="datamodel.html#types"><em>The standard type hierarchy</em></a>):</p>
|
|
<pre>
|
|
<strong id="grammar-token-classdef">classdef </strong> ::= [<a class="reference internal" href="#grammar-token-decorators"><tt class="xref docutils literal"><span class="pre">decorators</span></tt></a>] "class" <a class="reference internal" href="#grammar-token-classname"><tt class="xref docutils literal"><span class="pre">classname</span></tt></a> [<a class="reference internal" href="#grammar-token-inheritance"><tt class="xref docutils literal"><span class="pre">inheritance</span></tt></a>] ":" <a class="reference internal" href="#grammar-token-suite"><tt class="xref docutils literal"><span class="pre">suite</span></tt></a>
|
|
<strong id="grammar-token-inheritance">inheritance</strong> ::= "(" [<a class="reference internal" href="#grammar-token-parameter_list"><tt class="xref docutils literal"><span class="pre">parameter_list</span></tt></a>] ")"
|
|
<strong id="grammar-token-classname">classname </strong> ::= <a class="reference internal" href="lexical_analysis.html#grammar-token-identifier"><tt class="xref docutils literal"><span class="pre">identifier</span></tt></a>
|
|
</pre>
|
|
<p>A class definition is an executable statement. The inheritance list usually
|
|
gives a list of base classes (see <a class="reference internal" href="datamodel.html#metaclasses"><em>Customizing class creation</em></a> for more advanced uses), so
|
|
each item in the list should evaluate to a class object which allows
|
|
subclassing. Classes without an inheritance list inherit, by default, from the
|
|
base class <a class="reference internal" href="../library/functions.html#object" title="object"><tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></a>; hence,</p>
|
|
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Foo</span><span class="p">:</span>
|
|
<span class="k">pass</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>is equivalent to</p>
|
|
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="k">pass</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The class’s suite is then executed in a new execution frame (see <a class="reference internal" href="executionmodel.html#naming"><em>Naming and binding</em></a>),
|
|
using a newly created local namespace and the original global namespace.
|
|
(Usually, the suite contains mostly function definitions.) When the class’s
|
|
suite finishes execution, its execution frame is discarded but its local
|
|
namespace is saved. <a class="footnote-reference" href="#id8" id="id4">[4]</a> A class object is then created using the inheritance
|
|
list for the base classes and the saved local namespace for the attribute
|
|
dictionary. The class name is bound to this class object in the original local
|
|
namespace.</p>
|
|
<p>Class creation can be customized heavily using <a class="reference internal" href="datamodel.html#metaclasses"><em>metaclasses</em></a>.</p>
|
|
<p>Classes can also be decorated: just like when decorating functions,</p>
|
|
<div class="highlight-python3"><div class="highlight"><pre><span class="nd">@f1</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
|
|
<span class="nd">@f2</span>
|
|
<span class="k">class</span> <span class="nc">Foo</span><span class="p">:</span> <span class="k">pass</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>is equivalent to</p>
|
|
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Foo</span><span class="p">:</span> <span class="k">pass</span>
|
|
<span class="n">Foo</span> <span class="o">=</span> <span class="n">f1</span><span class="p">(</span><span class="n">arg</span><span class="p">)(</span><span class="n">f2</span><span class="p">(</span><span class="n">Foo</span><span class="p">))</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The evaluation rules for the decorator expressions are the same as for function
|
|
decorators. The result must be a class object, which is then bound to the class
|
|
name.</p>
|
|
<p><strong>Programmer’s note:</strong> Variables defined in the class definition are class
|
|
attributes; they are shared by instances. Instance attributes can be set in a
|
|
method with <tt class="docutils literal"><span class="pre">self.name</span> <span class="pre">=</span> <span class="pre">value</span></tt>. Both class and instance attributes are
|
|
accessible through the notation “<tt class="docutils literal"><span class="pre">self.name</span></tt>”, and an instance attribute hides
|
|
a class attribute with the same name when accessed in this way. Class
|
|
attributes can be used as defaults for instance attributes, but using mutable
|
|
values there can lead to unexpected results. <a class="reference internal" href="datamodel.html#descriptors"><em>Descriptors</em></a>
|
|
can be used to create instance variables with different implementation details.</p>
|
|
<div class="admonition seealso">
|
|
<p class="first admonition-title">See also</p>
|
|
<p class="last"><span class="target" id="index-27"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3115"><strong>PEP 3115</strong></a> - Metaclasses in Python 3
|
|
<span class="target" id="index-28"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3129"><strong>PEP 3129</strong></a> - Class Decorators</p>
|
|
</div>
|
|
<p class="rubric">Footnotes</p>
|
|
<table class="docutils footnote" frame="void" id="id5" rules="none">
|
|
<colgroup><col class="label" /><col /></colgroup>
|
|
<tbody valign="top">
|
|
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>The exception is propagated to the invocation stack unless
|
|
there is a <a class="reference internal" href="#finally"><tt class="xref std std-keyword docutils literal"><span class="pre">finally</span></tt></a> clause which happens to raise another
|
|
exception. That new exception causes the old one to be lost.</td></tr>
|
|
</tbody>
|
|
</table>
|
|
<table class="docutils footnote" frame="void" id="id6" rules="none">
|
|
<colgroup><col class="label" /><col /></colgroup>
|
|
<tbody valign="top">
|
|
<tr><td class="label"><a class="fn-backref" href="#id2">[2]</a></td><td>Currently, control “flows off the end” except in the case of an exception
|
|
or the execution of a <a class="reference internal" href="simple_stmts.html#return"><tt class="xref std std-keyword docutils literal"><span class="pre">return</span></tt></a>, <a class="reference internal" href="simple_stmts.html#continue"><tt class="xref std std-keyword docutils literal"><span class="pre">continue</span></tt></a>, or
|
|
<a class="reference internal" href="simple_stmts.html#break"><tt class="xref std std-keyword docutils literal"><span class="pre">break</span></tt></a> statement.</td></tr>
|
|
</tbody>
|
|
</table>
|
|
<table class="docutils footnote" frame="void" id="id7" rules="none">
|
|
<colgroup><col class="label" /><col /></colgroup>
|
|
<tbody valign="top">
|
|
<tr><td class="label"><a class="fn-backref" href="#id3">[3]</a></td><td>A string literal appearing as the first statement in the function body is
|
|
transformed into the function’s <tt class="docutils literal"><span class="pre">__doc__</span></tt> attribute and therefore the
|
|
function’s <a class="reference internal" href="../glossary.html#term-docstring"><em class="xref std std-term">docstring</em></a>.</td></tr>
|
|
</tbody>
|
|
</table>
|
|
<table class="docutils footnote" frame="void" id="id8" rules="none">
|
|
<colgroup><col class="label" /><col /></colgroup>
|
|
<tbody valign="top">
|
|
<tr><td class="label"><a class="fn-backref" href="#id4">[4]</a></td><td>A string literal appearing as the first statement in the class body is
|
|
transformed into the namespace’s <tt class="docutils literal"><span class="pre">__doc__</span></tt> item and therefore the class’s
|
|
<a class="reference internal" href="../glossary.html#term-docstring"><em class="xref std std-term">docstring</em></a>.</td></tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|