Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / ref / objects.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="next" href="types.html" />
13<link rel="prev" href="datamodel.html" />
14<link rel="parent" href="datamodel.html" />
15<link rel="next" href="types.html" />
16<meta name='aesop' content='information' />
17<title>3.1 Objects, values and types</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. Data model"
25 href="datamodel.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. Data model"
28 href="datamodel.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.2 The standard type"
31 href="types.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="datamodel.html">3. Data model</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="datamodel.html">3. Data model</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="types.html">3.2 The standard type</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION005100000000000000000"></A><A NAME="objects"></A><a id='l2h-19' xml:id='l2h-19'></a>
56<BR>
573.1 Objects, values and types
58</H1>
59
60<P>
61<i class="dfn">Objects</i> are Python's abstraction for data. All data in a Python
62program is represented by objects or by relations between objects.
63(In a sense, and in conformance to Von Neumann's model of a
64``stored program computer,'' code is also represented by objects.)
65
66<P>
67Every object has an identity, a type and a value. An object's
68<em>identity</em> never changes once it has been created; you may think
69of it as the object's address in memory. The `<tt class="keyword">is</tt>' operator
70compares the identity of two objects; the
71<tt class="function">id()</tt><a id='l2h-20' xml:id='l2h-20'></a> function returns an integer
72representing its identity (currently implemented as its address).
73An object's <i class="dfn">type</i> is
74also unchangeable.<A NAME="tex2html2"
75 HREF="#foot2364"><SUP>3.1</SUP></A>An object's type determines the operations that the object
76supports (e.g., ``does it have a length?'') and also defines the
77possible values for objects of that type. The
78<tt class="function">type()</tt><a id='l2h-21' xml:id='l2h-21'></a> function returns an object's type
79(which is an object itself). The <em>value</em> of some
80objects can change. Objects whose value can change are said to be
81<em>mutable</em>; objects whose value is unchangeable once they are
82created are called <em>immutable</em>.
83(The value of an immutable container object that contains a reference
84to a mutable object can change when the latter's value is changed;
85however the container is still considered immutable, because the
86collection of objects it contains cannot be changed. So, immutability
87is not strictly the same as having an unchangeable value, it is more
88subtle.)
89An object's mutability is determined by its type; for instance,
90numbers, strings and tuples are immutable, while dictionaries and
91lists are mutable.
92
93<P>
94Objects are never explicitly destroyed; however, when they become
95unreachable they may be garbage-collected. An implementation is
96allowed to postpone garbage collection or omit it altogether -- it is
97a matter of implementation quality how garbage collection is
98implemented, as long as no objects are collected that are still
99reachable. (Implementation note: the current implementation uses a
100reference-counting scheme with (optional) delayed detection of
101cyclically linked garbage, which collects most objects as soon as they
102become unreachable, but is not guaranteed to collect garbage
103containing circular references. See the
104<em class="citetitle"><a
105 href="../lib/module-gc.html"
106 title="Python Library Reference"
107 >Python Library Reference</a></em> for
108information on controlling the collection of cyclic garbage.)
109
110<P>
111Note that the use of the implementation's tracing or debugging
112facilities may keep objects alive that would normally be collectable.
113Also note that catching an exception with a
114`<tt class="keyword">try</tt>...<tt class="keyword">except</tt>' statement may keep objects alive.
115
116<P>
117Some objects contain references to ``external'' resources such as open
118files or windows. It is understood that these resources are freed
119when the object is garbage-collected, but since garbage collection is
120not guaranteed to happen, such objects also provide an explicit way to
121release the external resource, usually a <tt class="method">close()</tt> method.
122Programs are strongly recommended to explicitly close such
123objects. The `<tt class="keyword">try</tt>...<tt class="keyword">finally</tt>' statement provides
124a convenient way to do this.
125
126<P>
127Some objects contain references to other objects; these are called
128<em>containers</em>. Examples of containers are tuples, lists and
129dictionaries. The references are part of a container's value. In
130most cases, when we talk about the value of a container, we imply the
131values, not the identities of the contained objects; however, when we
132talk about the mutability of a container, only the identities of
133the immediately contained objects are implied. So, if an immutable
134container (like a tuple)
135contains a reference to a mutable object, its value changes
136if that mutable object is changed.
137
138<P>
139Types affect almost all aspects of object behavior. Even the importance
140of object identity is affected in some sense: for immutable types,
141operations that compute new values may actually return a reference to
142any existing object with the same type and value, while for mutable
143objects this is not allowed. E.g., after
144"<tt class="samp">a = 1; b = 1</tt>",
145<code>a</code> and <code>b</code> may or may not refer to the same object with the
146value one, depending on the implementation, but after
147"<tt class="samp">c = []; d = []</tt>", <code>c</code> and <code>d</code>
148are guaranteed to refer to two different, unique, newly created empty
149lists.
150(Note that "<tt class="samp">c = d = []</tt>" assigns the same object to both
151<code>c</code> and <code>d</code>.)
152
153<P>
154<BR><HR><H4>Footnotes</H4>
155<DL>
156<DT><A NAME="foot2364">... unchangeable.</A><A
157 href="objects.html#tex2html2"><SUP>3.1</SUP></A></DT>
158<DD>Since Python 2.2, a gradual merging of
159types and classes has been started that makes this and a few other
160assertions made in this manual not 100% accurate and complete:
161for example, it <em>is</em> now possible in some cases to change an
162object's type, under certain controlled conditions. Until this manual
163undergoes extensive revision, it must now be taken as authoritative
164only regarding ``classic classes'', that are still the default, for
165compatibility purposes, in Python 2.2 and 2.3.
166
167</DD>
168</DL>
169<DIV CLASS="navigation">
170<div class='online-navigation'>
171<p></p><hr />
172<table align="center" width="100%" cellpadding="0" cellspacing="2">
173<tr>
174<td class='online-navigation'><a rel="prev" title="3. Data model"
175 href="datamodel.html"><img src='../icons/previous.png'
176 border='0' height='32' alt='Previous Page' width='32' /></A></td>
177<td class='online-navigation'><a rel="parent" title="3. Data model"
178 href="datamodel.html"><img src='../icons/up.png'
179 border='0' height='32' alt='Up One Level' width='32' /></A></td>
180<td class='online-navigation'><a rel="next" title="3.2 The standard type"
181 href="types.html"><img src='../icons/next.png'
182 border='0' height='32' alt='Next Page' width='32' /></A></td>
183<td align="center" width="100%">Python Reference Manual</td>
184<td class='online-navigation'><a rel="contents" title="Table of Contents"
185 href="contents.html"><img src='../icons/contents.png'
186 border='0' height='32' alt='Contents' width='32' /></A></td>
187<td class='online-navigation'><img src='../icons/blank.png'
188 border='0' height='32' alt='' width='32' /></td>
189<td class='online-navigation'><a rel="index" title="Index"
190 href="genindex.html"><img src='../icons/index.png'
191 border='0' height='32' alt='Index' width='32' /></A></td>
192</tr></table>
193<div class='online-navigation'>
194<b class="navlabel">Previous:</b>
195<a class="sectref" rel="prev" href="datamodel.html">3. Data model</A>
196<b class="navlabel">Up:</b>
197<a class="sectref" rel="parent" href="datamodel.html">3. Data model</A>
198<b class="navlabel">Next:</b>
199<a class="sectref" rel="next" href="types.html">3.2 The standard type</A>
200</div>
201</div>
202<hr />
203<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
204</DIV>
205<!--End of Navigation Panel-->
206<ADDRESS>
207See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
208</ADDRESS>
209</BODY>
210</HTML>