~ubuntu-branches/debian/lenny/epydoc/lenny

« back to all changes in this revision

Viewing changes to doc/manual-docstring.html

  • Committer: Bazaar Package Importer
  • Author(s): Kenneth J. Pronovici
  • Date: 2008-02-03 13:22:12 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080203132212-u2uohl6rswmlz2ra
Tags: 3.0.1-1
* New upstream release.
* Removed #! from top of epydoc/gui.py
* Got rid of version mangling in debian/watch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="ascii" ?>
 
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
4
<head>
 
5
<meta http-equiv="Content-Type" content="text/html; charset=ascii" />
 
6
<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
 
7
<title>Python Docstrings</title>
 
8
<link rel="stylesheet" href="custom.css" type="text/css" />
 
9
</head>
 
10
<body>
 
11
<div class="document" id="python-docstrings">
 
12
<h1 class="title">Python Docstrings</h1>
 
13
 
 
14
<!-- $Id: manual-docstring.txt 1575 2007-03-08 21:28:07Z edloper $ -->
 
15
<p>Python documentation strings (or <em>docstrings</em>) provide a convenient way of
 
16
associating documentation with Python modules, functions, classes, and methods.
 
17
An object's docsting is defined by including a string constant as the first
 
18
statement in the object's definition. For example, the following function
 
19
defines a docstring:</p>
 
20
<pre class="py-doctest">
 
21
<span class="py-keyword">def</span> <span class="py-defname">x_intercept</span>(m, b):
 
22
    <span class="py-string">&quot;&quot;&quot;</span>
 
23
<span class="py-string">    Return the x intercept of the line y=m*x+b.  The x intercept of a</span>
 
24
<span class="py-string">    line is the point at which it crosses the x axis (y=0).</span>
 
25
<span class="py-string">    &quot;&quot;&quot;</span>
 
26
    return -b/m</pre>
 
27
<p>Docstrings can be accessed from the interpreter and from Python programs
 
28
using the &quot;<tt class="docutils literal"><span class="pre">__doc__</span></tt>&quot; attribute:</p>
 
29
<pre class="py-doctest">
 
30
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">print</span> x_intercept.__doc__
 
31
    Return the x intercept of the line y=m*x+b.  The x intercept of a
 
32
    line <span class="py-keyword">is</span> the point at which it crosses the x axis (y=0).</pre>
 
33
<p>The <a class="reference external" href="http://web.lfw.org/python/pydoc.html">pydoc</a> module, which became part of <a class="reference external" href="http://www.python.org/doc/current/lib/module-pydoc.html">the standard library</a> in Python 2.1,
 
34
can be used to display information about a Python object, including its
 
35
docstring:</p>
 
36
<pre class="py-doctest">
 
37
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">from</span> pydoc <span class="py-keyword">import</span> help
 
38
 
 
39
<span class="py-prompt">&gt;&gt;&gt; </span>help(x_intercept)
 
40
Help on function x_intercept <span class="py-keyword">in</span> module __main__:
 
41
 
 
42
x_intercept(m, b)
 
43
    Return the x intercept of the line y=m*x+b.  The x intercept of a
 
44
    line <span class="py-keyword">is</span> the point at which it crosses the x axis (y=0).</pre>
 
45
<p>For more information about Python docstrings, see the <a class="reference external" href="http://www.python.org/doc/current/tut/node6.html#docstrings">Python Tutorial</a> or
 
46
the O'Reilly Network article <a class="reference external" href="http://www.onlamp.com/lpt/a/python/2001/05/17/docstrings.html">Python Documentation Tips and Tricks</a>.</p>
 
47
<div class="section" id="variable-docstrings">
 
48
<h1>Variable docstrings</h1>
 
49
<!-- [xx] this should be somewhere else, i guess... -->
 
50
<p>Python don't support directly docstrings on variables: there is no attribute
 
51
that can be attached to variables and retrieved interactively like the
 
52
<tt class="docutils literal"><span class="pre">__doc__</span></tt> attribute on modules, classes and functions.</p>
 
53
<p>While the language doesn't directly provides for them, Epydoc supports
 
54
<em>variable docstrings</em>: if a variable assignment statement is immediately
 
55
followed by a bare string literal, then that assignment is treated as a
 
56
docstring for that variable. In classes, variable assignments at the class
 
57
definition level are considered class variables; and assignments to instance
 
58
variables in the constructor (<tt class="docutils literal"><span class="pre">__init__</span></tt>) are considered instance variables:</p>
 
59
<pre class="py-doctest">
 
60
<span class="py-keyword">class</span> <span class="py-defname">A</span>:
 
61
    x = 22
 
62
    <span class="py-string">&quot;&quot;&quot;Docstring for class variable A.x&quot;&quot;&quot;</span>
 
63
 
 
64
    <span class="py-keyword">def</span> <span class="py-defname">__init__</span>(self, a):
 
65
        self.y = a
 
66
        <span class="py-string">&quot;&quot;</span>&quot;Docstring <span class="py-keyword">for</span> instance variable A.y</pre>
 
67
<p>Variables may also be documented using <em>comment docstrings</em>. If a variable
 
68
assignment is immediately preceeded by a comment whose lines begin with the
 
69
special marker '<tt class="docutils literal"><span class="pre">#:</span></tt>', or is followed on the same line by such a comment,
 
70
then it is treated as a docstring for that variable:</p>
 
71
<pre class="py-doctest">
 
72
<span class="py-comment">#: docstring for x</span>
 
73
x = 22
 
74
x = 22 <span class="py-comment">#: docstring for x</span></pre>
 
75
<p>Notice that variable docstrings are only available for documentation when the
 
76
source code is available for <em>parsing</em>: it is not possible to retrieve variable</p>
 
77
</div>
 
78
<div class="section" id="items-visibility">
 
79
<h1>Items visibility</h1>
 
80
<p>Any Python object (modules, classes, functions, variables...) can be <em>public</em>
 
81
or <em>private</em>. Usually the object name decides the object visibility: objects
 
82
whose name starts with an underscore and doesn't end with an underscore are
 
83
considered private. All the other objects (including the &quot;magic functions&quot; such
 
84
as <tt class="docutils literal"><span class="pre">__add__</span></tt>) are public.</p>
 
85
<p>For each module and class, Epydoc generates pages with both public and private
 
86
methods. A Javascript snippet allows you to toggle the visibility of private
 
87
objects.</p>
 
88
<p>If a module wants to hide some of the objects it contains (either defined in
 
89
the module itself or imported from other modules), it can explicitly list the
 
90
names if its <a class="reference external" href="http://www.python.org/doc/2.4.3/ref/import.html">public names</a> in the <tt class="docutils literal"><span class="pre">__all__</span></tt> variable.</p>
 
91
<p>If a module defines the <tt class="docutils literal"><span class="pre">__all__</span></tt> variable, Epydoc uses its content to decide
 
92
if the module objects are public or private.</p>
 
93
</div>
 
94
</div>
 
95
<table width="100%" class="navbox" cellpadding="1" cellspacing="0">
 
96
  <tr>
 
97
  <a class="nav" href="index.html">
 
98
    <td align="center" width="20%" class="nav">
 
99
    <a class="nav" href="index.html">
 
100
    Home</a></td></a>
 
101
  <a class="nav" href="installing.html">
 
102
    <td align="center" width="20%" class="nav">
 
103
    <a class="nav" href="installing.html">
 
104
    Installing Epydoc</a></td></a>
 
105
  <a class="nav" href="using.html">
 
106
    <td align="center" width="20%" class="nav">
 
107
    <a class="nav" href="using.html">
 
108
    Using Epydoc</a></td></a>
 
109
  <a class="nav" href="epytext.html">
 
110
    <td align="center" width="20%" class="nav">
 
111
    <a class="nav" href="epytext.html">
 
112
    Epytext</a></td></a>
 
113
  <td align="center" width="20%" class="nav">
 
114
    
 
115
    <A href="http://sourceforge.net/projects/epydoc"> 
 
116
    <IMG src="sflogo.png" 
 
117
    width="88" height="26" border="0" alt="SourceForge"
 
118
    align="top"/></A></td>
 
119
    </tr>
 
120
</table>
 
121
</body>
 
122
</html>