/usr/share/doc/diveintopython/html/object_oriented_framework/instantiating_classes.html is in diveintopython 5.4-2ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | <!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>5.4. Instantiating Classes</title>
<link rel="stylesheet" href="../diveintopython.css" type="text/css">
<link rev="made" href="mailto:f8dy@diveintopython.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
<meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
<meta name="description" content="Python from novice to pro">
<link rel="home" href="../toc/index.html" title="Dive Into Python">
<link rel="up" href="index.html" title="Chapter 5. Objects and Object-Orientation">
<link rel="previous" href="defining_classes.html" title="5.3. Defining Classes">
<link rel="next" href="userdict.html" title="5.5. Exploring UserDict: A Wrapper Class">
</head>
<body>
<table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
<tr>
<td id="breadcrumb" colspan="5" align="left" valign="top">You are here: <a href="../index.html">Home</a> > <a href="../toc/index.html">Dive Into Python</a> > <a href="index.html">Objects and Object-Orientation</a> > <span class="thispage">Instantiating Classes</span></td>
<td id="navigation" align="right" valign="top"> <a href="defining_classes.html" title="Prev: “Defining Classes”"><<</a> <a href="userdict.html" title="Next: “Exploring UserDict: A Wrapper Class”">>></a></td>
</tr>
<tr>
<td colspan="3" id="logocontainer">
<h1 id="logo"><a href="../index.html" accesskey="1">Dive Into Python</a></h1>
<p id="tagline">Python from novice to pro</p>
</td>
<td colspan="3" align="right">
<form id="search" method="GET" action="http://www.google.com/custom">
<p><label for="q" accesskey="4">Find: </label><input type="text" id="q" name="q" size="20" maxlength="255" value=" "> <input type="submit" value="Search"><input type="hidden" name="cof" value="LW:752;L:http://diveintopython.org/images/diveintopython.png;LH:42;AH:left;GL:0;AWFID:3ced2bb1f7f1b212;"><input type="hidden" name="domains" value="diveintopython.org"><input type="hidden" name="sitesearch" value="diveintopython.org"></p>
</form>
</td>
</tr>
</table>
<!--#include virtual="/inc/ads" -->
<div class="section" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a name="fileinfo.create"></a>5.4. Instantiating Classes
</h2>
</div>
</div>
<div></div>
</div>
<div class="toc">
<ul>
<li><span class="section"><a href="instantiating_classes.html#d0e12165">5.4.1. Garbage Collection</a></span></li>
</ul>
</div>
<div class="abstract">
<p>Instantiating classes in <span class="application">Python</span> is straightforward. To instantiate a class, simply call the class as if it were a function, passing the arguments that the
<tt class="function">__init__</tt> method defines. The return value will be the newly created object.
</p>
</div>
<div class="example"><a name="d0e12003"></a><h3 class="title">Example 5.7. Creating a <tt class="classname">FileInfo</tt> Instance
</h3><pre class="screen"><tt class="prompt">>>> </tt><span class="userinput"><span class='pykeyword'>import</span> fileinfo</span>
<tt class="prompt">>>> </tt><span class="userinput">f = fileinfo.FileInfo(<span class='pystring'>"/music/_singles/kairo.mp3"</span>)</span> <a name="fileinfo.create.1.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">>>> </tt><span class="userinput">f.__class__</span> <a name="fileinfo.create.1.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput"><class fileinfo.FileInfo at 010EC204></span>
<tt class="prompt">>>> </tt><span class="userinput">f.__doc__</span> <a name="fileinfo.create.1.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<span class="computeroutput">'store file metadata'</span>
<tt class="prompt">>>> </tt><span class="userinput">f</span> <a name="fileinfo.create.1.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput">{'name': '/music/_singles/kairo.mp3'}</span></pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href="#fileinfo.create.1.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">You are creating an instance of the <tt class="classname">FileInfo</tt> class (defined in the <tt class="filename">fileinfo</tt> module) and assigning the newly created instance to the variable <tt class="varname">f</tt>. You are passing one parameter, <tt class="literal">/music/_singles/kairo.mp3</tt>, which will end up as the <tt class="varname">filename</tt> argument in <tt class="classname">FileInfo</tt>'s <tt class="function">__init__</tt> method.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#fileinfo.create.1.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Every class instance has a built-in attribute, <tt class="literal">__class__</tt>, which is the object's class. (Note that the representation of this includes the physical address of the instance on my
machine; your representation will be different.) <span class="application">Java</span> programmers may be familiar with the <tt class="classname">Class</tt> class, which contains methods like <tt class="function">getName</tt> and <tt class="function">getSuperclass</tt> to get metadata information about an object. In <span class="application">Python</span>, this kind of metadata is available directly on the object itself through attributes like <tt class="literal">__class__</tt>, <tt class="literal">__name__</tt>, and <tt class="literal">__bases__</tt>.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#fileinfo.create.1.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">You can access the instance's <tt class="literal">doc string</tt> just as with a function or a module. All instances of a class share the same <tt class="literal">doc string</tt>.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#fileinfo.create.1.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Remember when the <tt class="function">__init__</tt> method <a href="defining_classes.html#fileinfo.class.example" title="Example 5.4. Defining the FileInfo Class">assigned its <tt class="varname">filename</tt> argument to <tt class="literal">self["name"]</tt></a>? Well, here's the result. The arguments you pass when you create the class instance get sent right along to the <tt class="function">__init__</tt> method (along with the object reference, <tt class="literal">self</tt>, which <span class="application">Python</span> adds for free).
</td>
</tr>
</table>
</div>
</div><a name="compare.new.java"></a><table class="note" border="0" summary="">
<tr>
<td rowspan="2" align="center" valign="top" width="1%"><img src="../images/note.png" alt="Note" title="" width="24" height="24"></td>
</tr>
<tr>
<td colspan="2" align="left" valign="top" width="99%">In <span class="application">Python</span>, simply call a class as if it were a function to create a new instance of the class. There is no explicit <tt class="literal">new</tt> operator like <span class="application"><span class="acronym">C++</span></span> or <span class="application">Java</span>.
</td>
</tr>
</table>
<div class="section" lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="d0e12165"></a>5.4.1. Garbage Collection
</h3>
</div>
</div>
<div></div>
</div>
<p>If creating new instances is easy, destroying them is even easier. In general, there is no need to explicitly free instances,
because they are freed automatically when the variables assigned to them go out of scope. Memory leaks are rare in <span class="application">Python</span>.
</p>
<div class="example"><a name="fileinfo.scope"></a><h3 class="title">Example 5.8. Trying to Implement a Memory Leak</h3><pre class="screen"><tt class="prompt">>>> </tt><span class="userinput"><span class='pykeyword'>def</span><span class='pyclass'> leakmem</span>():</span>
<tt class="prompt">... </tt><span class="userinput">f = fileinfo.FileInfo(<span class='pystring'>'/music/_singles/kairo.mp3'</span>)</span> <a name="fileinfo.create.2.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">... </tt>
<tt class="prompt">>>> </tt><span class="userinput"><span class='pykeyword'>for</span> i <span class='pykeyword'>in</span> range(100):</span>
<tt class="prompt">... </tt><span class="userinput">leakmem()</span> <a name="fileinfo.create.2.3"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href="#fileinfo.create.2.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Every time the <tt class="function">leakmem</tt> function is called, you are creating an instance of <tt class="classname">FileInfo</tt> and assigning it to the variable <tt class="varname">f</tt>, which is a local variable within the function. Then the function ends without ever freeing <tt class="varname">f</tt>, so you would expect a memory leak, but you would be wrong. When the function ends, the local variable <tt class="varname">f</tt> goes out of scope. At this point, there are no longer any references to the newly created instance of <tt class="classname">FileInfo</tt> (since you never assigned it to anything other than <tt class="varname">f</tt>), so <span class="application">Python</span> destroys the instance for us.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#fileinfo.create.2.3"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">No matter how many times you call the <tt class="function">leakmem</tt> function, it will never leak memory, because every time, <span class="application">Python</span> will destroy the newly created <tt class="classname">FileInfo</tt> class before returning from <tt class="function">leakmem</tt>.
</td>
</tr>
</table>
</div>
</div>
<p>The technical term for this form of garbage collection is “<span class="quote">reference counting</span>”. <span class="application">Python</span> keeps a list of references to every instance created. In the above example, there was only one reference to the <tt class="classname">FileInfo</tt> instance: the local variable <tt class="varname">f</tt>. When the function ends, the variable <tt class="varname">f</tt> goes out of scope, so the reference count drops to <tt class="constant">0</tt>, and <span class="application">Python</span> destroys the instance automatically.
</p>
<p>In previous versions of <span class="application">Python</span>, there were situations where reference counting failed, and <span class="application">Python</span> couldn't clean up after you. If you created two instances that referenced each other (for instance, a doubly-linked list,
where each node has a pointer to the previous and next node in the list), neither instance would ever be destroyed automatically
because <span class="application">Python</span> (correctly) believed that there is always a reference to each instance. <span class="application">Python</span> 2.0 has an additional form of garbage collection called “<span class="quote">mark-and-sweep</span>” which is smart enough to notice this virtual gridlock and clean up circular references correctly.
</p>
<p>As a former philosophy major, it disturbs me to think that things disappear when no one is looking at them, but that's exactly
what happens in <span class="application">Python</span>. In general, you can simply forget about memory management and let <span class="application">Python</span> clean up after you.
</p>
<div class="furtherreading">
<h3>Further Reading on Garbage Collection</h3>
<ul>
<li><a href="http://www.python.org/doc/current/lib/"><i class="citetitle"><span class="application">Python</span> Library Reference</i></a> summarizes <a href="http://www.python.org/doc/current/lib/specialattrs.html">built-in attributes like <tt class="literal">__class__</tt></a>.
</li>
<li><a href="http://www.python.org/doc/current/lib/"><i class="citetitle"><span class="application">Python</span> Library Reference</i></a> documents the <a href="http://www.python.org/doc/current/lib/module-gc.html"><tt class="filename">gc</tt> module</a>, which gives you low-level control over <span class="application">Python</span>'s garbage collection.
</li>
</ul>
</div>
</div>
</div>
<table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
<tr>
<td width="35%" align="left"><br><a class="NavigationArrow" href="defining_classes.html"><< Defining Classes</a></td>
<td width="30%" align="center"><br> <span class="divider">|</span> <a href="index.html#fileinfo.divein" title="5.1. Diving In">1</a> <span class="divider">|</span> <a href="importing_modules.html" title="5.2. Importing Modules Using from module import">2</a> <span class="divider">|</span> <a href="defining_classes.html" title="5.3. Defining Classes">3</a> <span class="divider">|</span> <span class="thispage">4</span> <span class="divider">|</span> <a href="userdict.html" title="5.5. Exploring UserDict: A Wrapper Class">5</a> <span class="divider">|</span> <a href="special_class_methods.html" title="5.6. Special Class Methods">6</a> <span class="divider">|</span> <a href="special_class_methods2.html" title="5.7. Advanced Special Class Methods">7</a> <span class="divider">|</span> <a href="class_attributes.html" title="5.8. Introducing Class Attributes">8</a> <span class="divider">|</span> <a href="private_functions.html" title="5.9. Private Functions">9</a> <span class="divider">|</span> <a href="summary.html" title="5.10. Summary">10</a> <span class="divider">|</span>
</td>
<td width="35%" align="right"><br><a class="NavigationArrow" href="userdict.html">Exploring UserDict: A Wrapper Class >></a></td>
</tr>
<tr>
<td colspan="3"><br></td>
</tr>
</table>
<div class="Footer">
<p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
</div>
</body>
</html>
|