Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / python / ref / customization.html
CommitLineData
86530b38
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="next" href="attribute-access.html" />
13<link rel="prev" href="specialnames.html" />
14<link rel="parent" href="specialnames.html" />
15<link rel="next" href="attribute-access.html" />
16<meta name='aesop' content='information' />
17<title>3.3.1 Basic customization</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 Special method names"
25 href="specialnames.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.2 Customizing attribute access"
31 href="attribute-access.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="specialnames.html">3.3 Special method names</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="attribute-access.html">3.3.2 Customizing attribute access</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H2><A NAME="SECTION005310000000000000000"></A><A NAME="customization"></A><a id='l2h-172' xml:id='l2h-172'></a>
56<BR>
573.3.1 Basic customization
58</H2>
59
60<P>
61<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
62 <td><nobr><b><tt id='l2h-173' xml:id='l2h-173' class="method">__new__</tt></b>(</nobr></td>
63 <td><var>cls</var><big>[</big><var>, ...</var><big>]</big><var></var>)</td></tr></table></dt>
64<dd>
65Called to create a new instance of class <var>cls</var>. <tt class="method">__new__()</tt>
66is a static method (special-cased so you need not declare it as such)
67that takes the class of which an instance was requested as its first
68argument. The remaining arguments are those passed to the object
69constructor expression (the call to the class). The return value of
70<tt class="method">__new__()</tt> should be the new object instance (usually an
71instance of <var>cls</var>).
72
73<P>
74Typical implementations create a new instance of the class by invoking
75the superclass's <tt class="method">__new__()</tt> method using
76"<tt class="samp">super(<var>currentclass</var>, <var>cls</var>).__new__(<var>cls</var>[, ...])</tt>"with appropriate arguments and then modifying the newly-created instance
77as necessary before returning it.
78
79<P>
80If <tt class="method">__new__()</tt> returns an instance of <var>cls</var>, then the new
81instance's <tt class="method">__init__()</tt> method will be invoked like
82"<tt class="samp">__init__(<var>self</var>[, ...])</tt>", where <var>self</var> is the new instance
83and the remaining arguments are the same as were passed to
84<tt class="method">__new__()</tt>.
85
86<P>
87If <tt class="method">__new__()</tt> does not return an instance of <var>cls</var>, then the
88new instance's <tt class="method">__init__()</tt> method will not be invoked.
89
90<P>
91<tt class="method">__new__()</tt> is intended mainly to allow subclasses of
92immutable types (like int, str, or tuple) to customize instance
93creation.
94</dl>
95
96<P>
97<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
98 <td><nobr><b><tt id='l2h-174' xml:id='l2h-174' class="method">__init__</tt></b>(</nobr></td>
99 <td><var>self</var><big>[</big><var>, ...</var><big>]</big><var></var>)</td></tr></table></dt>
100<dd>
101Called<a id='l2h-175' xml:id='l2h-175'></a> when the instance is created. The
102arguments are those passed to the class constructor expression. If a
103base class has an <tt class="method">__init__()</tt> method, the derived class's
104<tt class="method">__init__()</tt> method, if any, must explicitly call it to ensure proper
105initialization of the base class part of the instance; for example:
106"<tt class="samp">BaseClass.__init__(<var>self</var>, [<var>args</var>...])</tt>". As a special
107constraint on constructors, no value may be returned; doing so will
108cause a <tt class="exception">TypeError</tt> to be raised at runtime.
109</dl>
110
111<P>
112<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
113 <td><nobr><b><tt id='l2h-176' xml:id='l2h-176' class="method">__del__</tt></b>(</nobr></td>
114 <td><var>self</var>)</td></tr></table></dt>
115<dd>
116Called when the instance is about to be destroyed. This is also
117called a destructor<a id='l2h-205' xml:id='l2h-205'></a>. If a base class
118has a <tt class="method">__del__()</tt> method, the derived class's <tt class="method">__del__()</tt>
119method, if any,
120must explicitly call it to ensure proper deletion of the base class
121part of the instance. Note that it is possible (though not recommended!)
122for the <tt class="method">__del__()</tt>
123method to postpone destruction of the instance by creating a new
124reference to it. It may then be called at a later time when this new
125reference is deleted. It is not guaranteed that
126<tt class="method">__del__()</tt> methods are called for objects that still exist when
127the interpreter exits.
128<a id='l2h-177' xml:id='l2h-177'></a>
129<P>
130<div class="note"><b class="label">Note:</b>
131"<tt class="samp">del x</tt>" doesn't directly call
132<code>x.__del__()</code> -- the former decrements the reference count for
133<code>x</code> by one, and the latter is only called when <code>x</code>'s reference
134count reaches zero. Some common situations that may prevent the
135reference count of an object from going to zero include: circular
136references between objects (e.g., a doubly-linked list or a tree data
137structure with parent and child pointers); a reference to the object
138on the stack frame of a function that caught an exception (the
139traceback stored in <code>sys.exc_traceback</code> keeps the stack frame
140alive); or a reference to the object on the stack frame that raised an
141unhandled exception in interactive mode (the traceback stored in
142<code>sys.last_traceback</code> keeps the stack frame alive). The first
143situation can only be remedied by explicitly breaking the cycles; the
144latter two situations can be resolved by storing <code>None</code> in
145<code>sys.exc_traceback</code> or <code>sys.last_traceback</code>. Circular
146references which are garbage are detected when the option cycle
147detector is enabled (it's on by default), but can only be cleaned up
148if there are no Python-level <tt class="method">__del__()</tt> methods involved.
149Refer to the documentation for the <a class="ulink" href="../lib/module-gc.html"
150 ><tt class="module">gc</tt>
151module</a> for more information about how
152<tt class="method">__del__()</tt> methods are handled by the cycle detector,
153particularly the description of the <code>garbage</code> value.
154</div>
155
156<P>
157<div class="warning"><b class="label">Warning:</b>
158
159Due to the precarious circumstances under which
160<tt class="method">__del__()</tt> methods are invoked, exceptions that occur during their
161execution are ignored, and a warning is printed to <code>sys.stderr</code>
162instead. Also, when <tt class="method">__del__()</tt> is invoked in response to a module
163being deleted (e.g., when execution of the program is done), other
164globals referenced by the <tt class="method">__del__()</tt> method may already have been
165deleted. For this reason, <tt class="method">__del__()</tt> methods should do the
166absolute minimum needed to maintain external invariants. Starting with
167version 1.5, Python guarantees that globals whose name begins with a single
168underscore are deleted from their module before other globals are deleted;
169if no other references to such globals exist, this may help in assuring that
170imported modules are still available at the time when the
171<tt class="method">__del__()</tt> method is called.
172</div>
173</dl>
174
175<P>
176<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
177 <td><nobr><b><tt id='l2h-178' xml:id='l2h-178' class="method">__repr__</tt></b>(</nobr></td>
178 <td><var>self</var>)</td></tr></table></dt>
179<dd>
180Called by the <tt class="function">repr()</tt><a id='l2h-179' xml:id='l2h-179'></a> built-in function
181and by string conversions (reverse quotes) to compute the ``official''
182string representation of an object. If at all possible, this should
183look like a valid Python expression that could be used to recreate an
184object with the same value (given an appropriate environment). If
185this is not possible, a string of the form "<tt class="samp">&lt;<var>...some useful
186description...</var>&gt;</tt>" should be returned. The return value must be a
187string object.
188If a class defines <tt class="method">__repr__()</tt> but not <tt class="method">__str__()</tt>,
189then <tt class="method">__repr__()</tt> is also used when an ``informal'' string
190representation of instances of that class is required.
191
192<P>
193This is typically used for debugging, so it is important that the
194representation is information-rich and unambiguous.
195<a id='l2h-180' xml:id='l2h-180'></a><a id='l2h-181' xml:id='l2h-181'></a><a id='l2h-182' xml:id='l2h-182'></a></dl>
196
197<P>
198<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
199 <td><nobr><b><tt id='l2h-183' xml:id='l2h-183' class="method">__str__</tt></b>(</nobr></td>
200 <td><var>self</var>)</td></tr></table></dt>
201<dd>
202Called by the <tt class="function">str()</tt><a id='l2h-184' xml:id='l2h-184'></a> built-in function and
203by the <tt class="keyword">print</tt><a id='l2h-185' xml:id='l2h-185'></a> statement to compute the
204``informal'' string representation of an object. This differs from
205<tt class="method">__repr__()</tt> in that it does not have to be a valid Python
206expression: a more convenient or concise representation may be used
207instead. The return value must be a string object.
208</dl>
209
210<P>
211<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
212 <td><nobr><b><tt id='l2h-186' xml:id='l2h-186' class="method">__lt__</tt></b>(</nobr></td>
213 <td><var>self, other</var>)</td></tr></table></dt>
214<dd>
215<dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
216 <td><nobr><b><tt id='l2h-187' xml:id='l2h-187' class="method">__le__</tt></b>(</nobr></td>
217 <td><var>self, other</var>)</td></tr></table></dt>
218<dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
219 <td><nobr><b><tt id='l2h-188' xml:id='l2h-188' class="method">__eq__</tt></b>(</nobr></td>
220 <td><var>self, other</var>)</td></tr></table></dt>
221<dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
222 <td><nobr><b><tt id='l2h-189' xml:id='l2h-189' class="method">__ne__</tt></b>(</nobr></td>
223 <td><var>self, other</var>)</td></tr></table></dt>
224<dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
225 <td><nobr><b><tt id='l2h-190' xml:id='l2h-190' class="method">__gt__</tt></b>(</nobr></td>
226 <td><var>self, other</var>)</td></tr></table></dt>
227<dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
228 <td><nobr><b><tt id='l2h-191' xml:id='l2h-191' class="method">__ge__</tt></b>(</nobr></td>
229 <td><var>self, other</var>)</td></tr></table></dt>
230<dd>
231<span class="versionnote">New in version 2.1.</span>
232
233These are the so-called ``rich comparison'' methods, and are called
234for comparison operators in preference to <tt class="method">__cmp__()</tt> below.
235The correspondence between operator symbols and method names is as
236follows:
237<code><var>x</var>&lt;<var>y</var></code> calls <code><var>x</var>.__lt__(<var>y</var>)</code>,
238<code><var>x</var>&lt;=<var>y</var></code> calls <code><var>x</var>.__le__(<var>y</var>)</code>,
239<code><var>x</var>==<var>y</var></code> calls <code><var>x</var>.__eq__(<var>y</var>)</code>,
240<code><var>x</var>!=<var>y</var></code> and <code><var>x</var>&lt;&gt;<var>y</var></code> call
241<code><var>x</var>.__ne__(<var>y</var>)</code>,
242<code><var>x</var>&gt;<var>y</var></code> calls <code><var>x</var>.__gt__(<var>y</var>)</code>, and
243<code><var>x</var>&gt;=<var>y</var></code> calls <code><var>x</var>.__ge__(<var>y</var>)</code>.
244These methods can return any value, but if the comparison operator is
245used in a Boolean context, the return value should be interpretable as
246a Boolean value, else a <tt class="exception">TypeError</tt> will be raised.
247By convention, <code>False</code> is used for false and <code>True</code> for true.
248
249<P>
250There are no implied relationships among the comparison operators.
251The truth of <code><var>x</var>==<var>y</var></code> does not imply that <code><var>x</var>!=<var>y</var></code>
252is false. Accordingly, when defining <tt class="method">__eq__()</tt>, one should also
253define <tt class="method">__ne__()</tt> so that the operators will behave as expected.
254
255<P>
256There are no reflected (swapped-argument) versions of these methods
257(to be used when the left argument does not support the operation but
258the right argument does); rather, <tt class="method">__lt__()</tt> and
259<tt class="method">__gt__()</tt> are each other's reflection, <tt class="method">__le__()</tt> and
260<tt class="method">__ge__()</tt> are each other's reflection, and <tt class="method">__eq__()</tt>
261and <tt class="method">__ne__()</tt> are their own reflection.
262
263<P>
264Arguments to rich comparison methods are never coerced. A rich
265comparison method may return <code>NotImplemented</code> if it does not
266implement the operation for a given pair of arguments.
267</dl>
268
269<P>
270<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
271 <td><nobr><b><tt id='l2h-192' xml:id='l2h-192' class="method">__cmp__</tt></b>(</nobr></td>
272 <td><var>self, other</var>)</td></tr></table></dt>
273<dd>
274Called by comparison operations if rich comparison (see above) is not
275defined. Should return a negative integer if <code>self &lt; other</code>,
276zero if <code>self == other</code>, a positive integer if <code>self &gt;
277other</code>. If no <tt class="method">__cmp__()</tt>, <tt class="method">__eq__()</tt> or
278<tt class="method">__ne__()</tt> operation is defined, class instances are compared
279by object identity (``address''). See also the description of
280<tt class="method">__hash__()</tt> for some important notes on creating objects which
281support custom comparison operations and are usable as dictionary
282keys.
283(Note: the restriction that exceptions are not propagated by
284<tt class="method">__cmp__()</tt> has been removed since Python 1.5.)
285<a id='l2h-193' xml:id='l2h-193'></a></dl>
286
287<P>
288<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
289 <td><nobr><b><tt id='l2h-194' xml:id='l2h-194' class="method">__rcmp__</tt></b>(</nobr></td>
290 <td><var>self, other</var>)</td></tr></table></dt>
291<dd>
292
293<span class="versionnote">Changed in version 2.1:
294No longer supported.</span>
295
296</dl>
297
298<P>
299<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
300 <td><nobr><b><tt id='l2h-195' xml:id='l2h-195' class="method">__hash__</tt></b>(</nobr></td>
301 <td><var>self</var>)</td></tr></table></dt>
302<dd>
303Called for the key object for dictionary <a id='l2h-196' xml:id='l2h-196'></a>operations, and by the built-in function
304<tt class="function">hash()</tt><a id='l2h-197' xml:id='l2h-197'></a>. Should return a 32-bit integer
305usable as a hash value
306for dictionary operations. The only required property is that objects
307which compare equal have the same hash value; it is advised to somehow
308mix together (e.g., using exclusive or) the hash values for the
309components of the object that also play a part in comparison of
310objects. If a class does not define a <tt class="method">__cmp__()</tt> method it should
311not define a <tt class="method">__hash__()</tt> operation either; if it defines
312<tt class="method">__cmp__()</tt> or <tt class="method">__eq__()</tt> but not <tt class="method">__hash__()</tt>,
313its instances will not be usable as dictionary keys. If a class
314defines mutable objects and implements a <tt class="method">__cmp__()</tt> or
315<tt class="method">__eq__()</tt> method, it should not implement <tt class="method">__hash__()</tt>,
316since the dictionary implementation requires that a key's hash value
317is immutable (if the object's hash value changes, it will be in the
318wrong hash bucket).
319<a id='l2h-199' xml:id='l2h-199'></a></dl>
320
321<P>
322<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
323 <td><nobr><b><tt id='l2h-200' xml:id='l2h-200' class="method">__nonzero__</tt></b>(</nobr></td>
324 <td><var>self</var>)</td></tr></table></dt>
325<dd>
326Called to implement truth value testing, and the built-in operation
327<code>bool()</code>; should return <code>False</code> or <code>True</code>, or their
328integer equivalents <code>0</code> or <code>1</code>.
329When this method is not defined, <tt class="method">__len__()</tt> is
330called, if it is defined (see below). If a class defines neither
331<tt class="method">__len__()</tt> nor <tt class="method">__nonzero__()</tt>, all its instances are
332considered true.
333<a id='l2h-202' xml:id='l2h-202'></a></dl>
334
335<P>
336<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
337 <td><nobr><b><tt id='l2h-203' xml:id='l2h-203' class="method">__unicode__</tt></b>(</nobr></td>
338 <td><var>self</var>)</td></tr></table></dt>
339<dd>
340Called to implement <tt class="function">unicode()</tt><a id='l2h-204' xml:id='l2h-204'></a> builtin;
341should return a Unicode object. When this method is not defined, string
342conversion is attempted, and the result of string conversion is converted
343to Unicode using the system default encoding.
344</dl>
345
346<P>
347
348<DIV CLASS="navigation">
349<div class='online-navigation'>
350<p></p><hr />
351<table align="center" width="100%" cellpadding="0" cellspacing="2">
352<tr>
353<td class='online-navigation'><a rel="prev" title="3.3 Special method names"
354 href="specialnames.html"><img src='../icons/previous.png'
355 border='0' height='32' alt='Previous Page' width='32' /></A></td>
356<td class='online-navigation'><a rel="parent" title="3.3 Special method names"
357 href="specialnames.html"><img src='../icons/up.png'
358 border='0' height='32' alt='Up One Level' width='32' /></A></td>
359<td class='online-navigation'><a rel="next" title="3.3.2 Customizing attribute access"
360 href="attribute-access.html"><img src='../icons/next.png'
361 border='0' height='32' alt='Next Page' width='32' /></A></td>
362<td align="center" width="100%">Python Reference Manual</td>
363<td class='online-navigation'><a rel="contents" title="Table of Contents"
364 href="contents.html"><img src='../icons/contents.png'
365 border='0' height='32' alt='Contents' width='32' /></A></td>
366<td class='online-navigation'><img src='../icons/blank.png'
367 border='0' height='32' alt='' width='32' /></td>
368<td class='online-navigation'><a rel="index" title="Index"
369 href="genindex.html"><img src='../icons/index.png'
370 border='0' height='32' alt='Index' width='32' /></A></td>
371</tr></table>
372<div class='online-navigation'>
373<b class="navlabel">Previous:</b>
374<a class="sectref" rel="prev" href="specialnames.html">3.3 Special method names</A>
375<b class="navlabel">Up:</b>
376<a class="sectref" rel="parent" href="specialnames.html">3.3 Special method names</A>
377<b class="navlabel">Next:</b>
378<a class="sectref" rel="next" href="attribute-access.html">3.3.2 Customizing attribute access</A>
379</div>
380</div>
381<hr />
382<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
383</DIV>
384<!--End of Navigation Panel-->
385<ADDRESS>
386See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
387</ADDRESS>
388</BODY>
389</HTML>