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="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> | |
57 | 3.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 | |
62 | program 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> | |
67 | Every 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 | |
69 | of it as the object's address in memory. The `<tt class="keyword">is</tt>' operator | |
70 | compares 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 | |
72 | representing its identity (currently implemented as its address). | |
73 | An object's <i class="dfn">type</i> is | |
74 | also unchangeable.<A NAME="tex2html2" | |
75 | HREF="#foot2364"><SUP>3.1</SUP></A>An object's type determines the operations that the object | |
76 | supports (e.g., ``does it have a length?'') and also defines the | |
77 | possible 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 | |
80 | objects can change. Objects whose value can change are said to be | |
81 | <em>mutable</em>; objects whose value is unchangeable once they are | |
82 | created are called <em>immutable</em>. | |
83 | (The value of an immutable container object that contains a reference | |
84 | to a mutable object can change when the latter's value is changed; | |
85 | however the container is still considered immutable, because the | |
86 | collection of objects it contains cannot be changed. So, immutability | |
87 | is not strictly the same as having an unchangeable value, it is more | |
88 | subtle.) | |
89 | An object's mutability is determined by its type; for instance, | |
90 | numbers, strings and tuples are immutable, while dictionaries and | |
91 | lists are mutable. | |
92 | ||
93 | <P> | |
94 | Objects are never explicitly destroyed; however, when they become | |
95 | unreachable they may be garbage-collected. An implementation is | |
96 | allowed to postpone garbage collection or omit it altogether -- it is | |
97 | a matter of implementation quality how garbage collection is | |
98 | implemented, as long as no objects are collected that are still | |
99 | reachable. (Implementation note: the current implementation uses a | |
100 | reference-counting scheme with (optional) delayed detection of | |
101 | cyclically linked garbage, which collects most objects as soon as they | |
102 | become unreachable, but is not guaranteed to collect garbage | |
103 | containing 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 | |
108 | information on controlling the collection of cyclic garbage.) | |
109 | ||
110 | <P> | |
111 | Note that the use of the implementation's tracing or debugging | |
112 | facilities may keep objects alive that would normally be collectable. | |
113 | Also 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> | |
117 | Some objects contain references to ``external'' resources such as open | |
118 | files or windows. It is understood that these resources are freed | |
119 | when the object is garbage-collected, but since garbage collection is | |
120 | not guaranteed to happen, such objects also provide an explicit way to | |
121 | release the external resource, usually a <tt class="method">close()</tt> method. | |
122 | Programs are strongly recommended to explicitly close such | |
123 | objects. The `<tt class="keyword">try</tt>...<tt class="keyword">finally</tt>' statement provides | |
124 | a convenient way to do this. | |
125 | ||
126 | <P> | |
127 | Some objects contain references to other objects; these are called | |
128 | <em>containers</em>. Examples of containers are tuples, lists and | |
129 | dictionaries. The references are part of a container's value. In | |
130 | most cases, when we talk about the value of a container, we imply the | |
131 | values, not the identities of the contained objects; however, when we | |
132 | talk about the mutability of a container, only the identities of | |
133 | the immediately contained objects are implied. So, if an immutable | |
134 | container (like a tuple) | |
135 | contains a reference to a mutable object, its value changes | |
136 | if that mutable object is changed. | |
137 | ||
138 | <P> | |
139 | Types affect almost all aspects of object behavior. Even the importance | |
140 | of object identity is affected in some sense: for immutable types, | |
141 | operations that compute new values may actually return a reference to | |
142 | any existing object with the same type and value, while for mutable | |
143 | objects 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 | |
146 | value one, depending on the implementation, but after | |
147 | "<tt class="samp">c = []; d = []</tt>", <code>c</code> and <code>d</code> | |
148 | are guaranteed to refer to two different, unique, newly created empty | |
149 | lists. | |
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 | |
159 | types and classes has been started that makes this and a few other | |
160 | assertions made in this manual not 100% accurate and complete: | |
161 | for example, it <em>is</em> now possible in some cases to change an | |
162 | object's type, under certain controlled conditions. Until this manual | |
163 | undergoes extensive revision, it must now be taken as authoritative | |
164 | only regarding ``classic classes'', that are still the default, for | |
165 | compatibility 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> | |
207 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
208 | </ADDRESS> | |
209 | </BODY> | |
210 | </HTML> |