Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / ref / slots.html
CommitLineData
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="descriptor-invocation.html" />
13<link rel="parent" href="attribute-access.html" />
14<link rel="next" href="metaclasses.html" />
15<meta name='aesop' content='information' />
16<title>3.3.2.4 __slots__</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.3.2.3 Invoking Descriptors"
24 href="descriptor-invocation.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.3.2 Customizing attribute access"
27 href="attribute-access.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.3 Customizing class creation"
30 href="metaclasses.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="descriptor-invocation.html">3.3.2.3 Invoking Descriptors</A>
45<b class="navlabel">Up:</b>
46<a class="sectref" rel="parent" href="attribute-access.html">3.3.2 Customizing attribute access</A>
47<b class="navlabel">Next:</b>
48<a class="sectref" rel="next" href="metaclasses.html">3.3.3 Customizing class creation</A>
49</div>
50<hr /></div>
51</DIV>
52<!--End of Navigation Panel-->
53
54<H3><A NAME="SECTION005324000000000000000"></A><A NAME="slots"></A>
55<BR>
563.3.2.4 __slots__
57</H3>
58
59<P>
60By default, instances of both old and new-style classes have a dictionary
61for attribute storage. This wastes space for objects having very few instance
62variables. The space consumption can become acute when creating large numbers
63of instances.
64
65<P>
66The default can be overridden by defining <var>__slots__</var> in a new-style class
67definition. The <var>__slots__</var> declaration takes a sequence of instance
68variables and reserves just enough space in each instance to hold a value
69for each variable. Space is saved because <var>__dict__</var> is not created for
70each instance.
71
72<P>
73<dl><dt><b><tt id='l2h-217' xml:id='l2h-217'>__slots__</tt></b></dt>
74<dd>
75This class variable can be assigned a string, iterable, or sequence of strings
76with variable names used by instances. If defined in a new-style class,
77<var>__slots__</var> reserves space for the declared variables
78and prevents the automatic creation of <var>__dict__</var> and <var>__weakref__</var>
79for each instance.
80
81<span class="versionnote">New in version 2.2.</span>
82
83</dd></dl>
84
85<P>
86Notes on using <var>__slots__</var>
87
88<P>
89
90<UL>
91<LI>Without a <var>__dict__</var> variable, instances cannot be assigned new
92variables not listed in the <var>__slots__</var> definition. Attempts to assign
93to an unlisted variable name raises <tt class="exception">AttributeError</tt>. If dynamic
94assignment of new variables is desired, then add <code>'__dict__'</code> to the
95sequence of strings in the <var>__slots__</var> declaration.
96
97<span class="versionnote">Changed in version 2.3:
98Previously, adding <code>'__dict__'</code> to the <var>__slots__</var>
99declaration would not enable the assignment of new attributes not
100specifically listed in the sequence of instance variable names.</span>
101
102<P>
103</LI>
104<LI>Without a <var>__weakref__</var> variable for each instance, classes
105defining <var>__slots__</var> do not support weak references to its instances.
106If weak reference support is needed, then add <code>'__weakref__'</code> to the
107sequence of strings in the <var>__slots__</var> declaration.
108
109<span class="versionnote">Changed in version 2.3:
110Previously, adding <code>'__weakref__'</code> to the <var>__slots__</var>
111declaration would not enable support for weak references.</span>
112
113<P>
114</LI>
115<LI><var>__slots__</var> are implemented at the class level by creating
116descriptors (<A href="descriptors.html#descriptors">3.3.2</A>) for each variable name. As a result,
117class attributes cannot be used to set default values for instance
118variables defined by <var>__slots__</var>; otherwise, the class attribute would
119overwrite the descriptor assignment.
120
121<P>
122</LI>
123<LI>If a class defines a slot also defined in a base class, the instance
124variable defined by the base class slot is inaccessible (except by retrieving
125its descriptor directly from the base class). This renders the meaning of the
126program undefined. In the future, a check may be added to prevent this.
127
128<P>
129</LI>
130<LI>The action of a <var>__slots__</var> declaration is limited to the class
131where it is defined. As a result, subclasses will have a <var>__dict__</var>
132unless they also define <var>__slots__</var>.
133
134<P>
135</LI>
136<LI><var>__slots__</var> do not work for classes derived from ``variable-length''
137built-in types such as <tt class="class">long</tt>, <tt class="class">str</tt> and <tt class="class">tuple</tt>.
138
139<P>
140</LI>
141<LI>Any non-string iterable may be assigned to <var>__slots__</var>.
142Mappings may also be used; however, in the future, special meaning may
143be assigned to the values corresponding to each key.
144
145<P>
146</LI>
147</UL>
148
149<P>
150
151<DIV CLASS="navigation">
152<div class='online-navigation'>
153<p></p><hr />
154<table align="center" width="100%" cellpadding="0" cellspacing="2">
155<tr>
156<td class='online-navigation'><a rel="prev" title="3.3.2.3 Invoking Descriptors"
157 href="descriptor-invocation.html"><img src='../icons/previous.png'
158 border='0' height='32' alt='Previous Page' width='32' /></A></td>
159<td class='online-navigation'><a rel="parent" title="3.3.2 Customizing attribute access"
160 href="attribute-access.html"><img src='../icons/up.png'
161 border='0' height='32' alt='Up One Level' width='32' /></A></td>
162<td class='online-navigation'><a rel="next" title="3.3.3 Customizing class creation"
163 href="metaclasses.html"><img src='../icons/next.png'
164 border='0' height='32' alt='Next Page' width='32' /></A></td>
165<td align="center" width="100%">Python Reference Manual</td>
166<td class='online-navigation'><a rel="contents" title="Table of Contents"
167 href="contents.html"><img src='../icons/contents.png'
168 border='0' height='32' alt='Contents' width='32' /></A></td>
169<td class='online-navigation'><img src='../icons/blank.png'
170 border='0' height='32' alt='' width='32' /></td>
171<td class='online-navigation'><a rel="index" title="Index"
172 href="genindex.html"><img src='../icons/index.png'
173 border='0' height='32' alt='Index' width='32' /></A></td>
174</tr></table>
175<div class='online-navigation'>
176<b class="navlabel">Previous:</b>
177<a class="sectref" rel="prev" href="descriptor-invocation.html">3.3.2.3 Invoking Descriptors</A>
178<b class="navlabel">Up:</b>
179<a class="sectref" rel="parent" href="attribute-access.html">3.3.2 Customizing attribute access</A>
180<b class="navlabel">Next:</b>
181<a class="sectref" rel="next" href="metaclasses.html">3.3.3 Customizing class creation</A>
182</div>
183</div>
184<hr />
185<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
186</DIV>
187<!--End of Navigation Panel-->
188<ADDRESS>
189See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
190</ADDRESS>
191</BODY>
192</HTML>