Commit | Line | Data |
---|---|---|
920dae64 AT |
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="types.html" /> | |
13 | <link rel="parent" href="datamodel.html" /> | |
14 | <link rel="next" href="customization.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>3.3 Special method names</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="3.2 The standard type" | |
24 | href="types.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="3. Data model" | |
27 | href="datamodel.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="3.3.1 Basic customization" | |
30 | href="customization.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="types.html">3.2 The standard type</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="datamodel.html">3. Data model</A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="customization.html">3.3.1 Basic customization</A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H1><A NAME="SECTION005300000000000000000"></A><A NAME="specialnames"></A> | |
55 | <BR> | |
56 | 3.3 Special method names | |
57 | </H1> | |
58 | ||
59 | <P> | |
60 | A class can implement certain operations that are invoked by special | |
61 | syntax (such as arithmetic operations or subscripting and slicing) by | |
62 | defining methods with special names.<a id='l2h-169' xml:id='l2h-169'></a>This is Python's approach to <i class="dfn">operator overloading</i>, allowing | |
63 | classes to define their own behavior with respect to language | |
64 | operators. For instance, if a class defines | |
65 | a method named <tt class="method">__getitem__()</tt>, and <code>x</code> is an instance of | |
66 | this class, then <code>x[i]</code> is equivalent to | |
67 | <code>x.__getitem__(i)</code>. Except where mentioned, attempts to execute | |
68 | an operation raise an exception when no appropriate method is defined. | |
69 | <a id='l2h-171' xml:id='l2h-171'></a> | |
70 | <P> | |
71 | When implementing a class that emulates any built-in type, it is | |
72 | important that the emulation only be implemented to the degree that it | |
73 | makes sense for the object being modelled. For example, some | |
74 | sequences may work well with retrieval of individual elements, but | |
75 | extracting a slice may not make sense. (One example of this is the | |
76 | <tt class="class">NodeList</tt> interface in the W3C's Document Object Model.) | |
77 | ||
78 | <P> | |
79 | ||
80 | <p><br /></p><hr class='online-navigation' /> | |
81 | <div class='online-navigation'> | |
82 | <!--Table of Child-Links--> | |
83 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
84 | ||
85 | <UL CLASS="ChildLinks"> | |
86 | <LI><A href="customization.html">3.3.1 Basic customization</a> | |
87 | <LI><A href="attribute-access.html">3.3.2 Customizing attribute access</a> | |
88 | <UL> | |
89 | <LI><A href="new-style-attribute-access.html">3.3.2.1 More attribute access for new-style classes</a> | |
90 | <LI><A href="descriptors.html">3.3.2.2 Implementing Descriptors</a> | |
91 | <LI><A href="descriptor-invocation.html">3.3.2.3 Invoking Descriptors</a> | |
92 | <LI><A href="slots.html">3.3.2.4 __slots__</a> | |
93 | </ul> | |
94 | <LI><A href="metaclasses.html">3.3.3 Customizing class creation</a> | |
95 | <LI><A href="callable-types.html">3.3.4 Emulating callable objects</a> | |
96 | <LI><A href="sequence-types.html">3.3.5 Emulating container types</a> | |
97 | <LI><A href="sequence-methods.html">3.3.6 Additional methods for emulation of sequence types</a> | |
98 | <LI><A href="numeric-types.html">3.3.7 Emulating numeric types</a> | |
99 | <LI><A href="coercion-rules.html">3.3.8 Coercion rules</a> | |
100 | </ul> | |
101 | <!--End of Table of Child-Links--> | |
102 | </div> | |
103 | ||
104 | <DIV CLASS="navigation"> | |
105 | <div class='online-navigation'> | |
106 | <p></p><hr /> | |
107 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
108 | <tr> | |
109 | <td class='online-navigation'><a rel="prev" title="3.2 The standard type" | |
110 | href="types.html"><img src='../icons/previous.png' | |
111 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
112 | <td class='online-navigation'><a rel="parent" title="3. Data model" | |
113 | href="datamodel.html"><img src='../icons/up.png' | |
114 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
115 | <td class='online-navigation'><a rel="next" title="3.3.1 Basic customization" | |
116 | href="customization.html"><img src='../icons/next.png' | |
117 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
118 | <td align="center" width="100%">Python Reference Manual</td> | |
119 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
120 | href="contents.html"><img src='../icons/contents.png' | |
121 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
122 | <td class='online-navigation'><img src='../icons/blank.png' | |
123 | border='0' height='32' alt='' width='32' /></td> | |
124 | <td class='online-navigation'><a rel="index" title="Index" | |
125 | href="genindex.html"><img src='../icons/index.png' | |
126 | border='0' height='32' alt='Index' width='32' /></A></td> | |
127 | </tr></table> | |
128 | <div class='online-navigation'> | |
129 | <b class="navlabel">Previous:</b> | |
130 | <a class="sectref" rel="prev" href="types.html">3.2 The standard type</A> | |
131 | <b class="navlabel">Up:</b> | |
132 | <a class="sectref" rel="parent" href="datamodel.html">3. Data model</A> | |
133 | <b class="navlabel">Next:</b> | |
134 | <a class="sectref" rel="next" href="customization.html">3.3.1 Basic customization</A> | |
135 | </div> | |
136 | </div> | |
137 | <hr /> | |
138 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
139 | </DIV> | |
140 | <!--End of Navigation Panel--> | |
141 | <ADDRESS> | |
142 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
143 | </ADDRESS> | |
144 | </BODY> | |
145 | </HTML> |