Skip to content

Commit b0f4c70

Browse files
author
georg.brandl
committed
Incorporate some suggestions by Tait Stevens.
git-svn-id: http://svn.python.org/projects/python/trunk@66447 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent e73bc47 commit b0f4c70

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

Doc/tutorial/classes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ the class's namespace when the class object was created. So, if the class
208208
definition looked like this::
209209

210210
class MyClass:
211-
"A simple example class"
211+
"""A simple example class"""
212212
i = 12345
213213
def f(self):
214214
return 'hello world'

Doc/tutorial/controlflow.rst

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Perhaps the most well-known statement type is the :keyword:`if` statement. For
1717
example::
1818

1919
>>> x = int(raw_input("Please enter an integer: "))
20+
Please enter an integer: 42
2021
>>> if x < 0:
2122
... x = 0
2223
... print 'Negative changed to zero'
@@ -26,7 +27,8 @@ example::
2627
... print 'Single'
2728
... else:
2829
... print 'More'
29-
...
30+
...
31+
More
3032

3133
There can be zero or more :keyword:`elif` parts, and the :keyword:`else` part is
3234
optional. The keyword ':keyword:`elif`' is short for 'else if', and is useful
@@ -161,7 +163,7 @@ The :keyword:`pass` statement does nothing. It can be used when a statement is
161163
required syntactically but the program requires no action. For example::
162164

163165
>>> while True:
164-
... pass # Busy-wait for keyboard interrupt
166+
... pass # Busy-wait for keyboard interrupt (Ctrl+C)
165167
...
166168

167169

@@ -192,14 +194,14 @@ boundary::
192194
The keyword :keyword:`def` introduces a function *definition*. It must be
193195
followed by the function name and the parenthesized list of formal parameters.
194196
The statements that form the body of the function start at the next line, and
195-
must be indented. The first statement of the function body can optionally be a
196-
string literal; this string literal is the function's documentation string, or
197-
:dfn:`docstring`.
197+
must be indented.
198198

199+
The first statement of the function body can optionally be a string literal;
200+
this string literal is the function's documentation string, or :dfn:`docstring`.
201+
(More about docstrings can be found in the section :ref:`tut-docstrings`.)
199202
There are tools which use docstrings to automatically produce online or printed
200203
documentation, or to let the user interactively browse through code; it's good
201-
practice to include docstrings in code that you write, so try to make a habit of
202-
it.
204+
practice to include docstrings in code that you write, so make a habit of it.
203205

204206
The *execution* of a function introduces a new symbol table used for the local
205207
variables of the function. More precisely, all variable assignments in a
@@ -228,12 +230,12 @@ mechanism::
228230
>>> f(100)
229231
1 1 2 3 5 8 13 21 34 55 89
230232

231-
You might object that ``fib`` is not a function but a procedure. In Python,
232-
like in C, procedures are just functions that don't return a value. In fact,
233-
technically speaking, procedures do return a value, albeit a rather boring one.
234-
This value is called ``None`` (it's a built-in name). Writing the value
235-
``None`` is normally suppressed by the interpreter if it would be the only value
236-
written. You can see it if you really want to using :keyword:`print`::
233+
Coming from other languages, you might object that ``fib`` is not a function but
234+
a procedure since it doesn't return a value. In fact, even functions without a
235+
:keyword:`return` statement do return a value, albeit a rather boring one. This
236+
value is called ``None`` (it's a built-in name). Writing the value ``None`` is
237+
normally suppressed by the interpreter if it would be the only value written.
238+
You can see it if you really want to using :keyword:`print`::
237239

238240
>>> fib(0)
239241
>>> print fib(0)
@@ -259,7 +261,7 @@ This example, as usual, demonstrates some new Python features:
259261

260262
* The :keyword:`return` statement returns with a value from a function.
261263
:keyword:`return` without an expression argument returns ``None``. Falling off
262-
the end of a procedure also returns ``None``.
264+
the end of a function also returns ``None``.
263265

264266
* The statement ``result.append(b)`` calls a *method* of the list object
265267
``result``. A method is a function that 'belongs' to an object and is named
@@ -400,21 +402,21 @@ list. (``*name`` must occur before ``**name``.) For example, if we define a
400402
function like this::
401403

402404
def cheeseshop(kind, *arguments, **keywords):
403-
print "-- Do you have any", kind, '?'
405+
print "-- Do you have any", kind, "?"
404406
print "-- I'm sorry, we're all out of", kind
405407
for arg in arguments: print arg
406-
print '-'*40
408+
print "-" * 40
407409
keys = keywords.keys()
408410
keys.sort()
409-
for kw in keys: print kw, ':', keywords[kw]
411+
for kw in keys: print kw, ":", keywords[kw]
410412

411413
It could be called like this::
412414

413-
cheeseshop('Limburger', "It's very runny, sir.",
415+
cheeseshop("Limburger", "It's very runny, sir.",
414416
"It's really very, VERY runny, sir.",
415-
client='John Cleese',
416417
shopkeeper='Michael Palin',
417-
sketch='Cheese Shop Sketch')
418+
client="John Cleese",
419+
sketch="Cheese Shop Sketch")
418420

419421
and of course it would print::
420422

@@ -442,8 +444,8 @@ Arbitrary Argument Lists
442444

443445
Finally, the least frequently used option is to specify that a function can be
444446
called with an arbitrary number of arguments. These arguments will be wrapped
445-
up in a tuple. Before the variable number of arguments, zero or more normal
446-
arguments may occur. ::
447+
up in a tuple (see :ref:`tut-tuples`). Before the variable number of arguments,
448+
zero or more normal arguments may occur. ::
447449

448450
def write_multiple_items(file, separator, *args):
449451
file.write(separator.join(args))
@@ -600,7 +602,8 @@ extracted for you:
600602

601603
* Name your classes and functions consistently; the convention is to use
602604
``CamelCase`` for classes and ``lower_case_with_underscores`` for functions
603-
and methods. Always use ``self`` as the name for the first method argument.
605+
and methods. Always use ``self`` as the name for the first method argument
606+
(see :ref:`tut-firstclasses` for more on classes and methods).
604607

605608
* Don't use fancy encodings if your code is meant to be used in international
606609
environments. Plain ASCII works best in any case.

Doc/tutorial/datastructures.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ of an empty list to the slice). For example::
345345
Referencing the name ``a`` hereafter is an error (at least until another value
346346
is assigned to it). We'll find other uses for :keyword:`del` later.
347347

348+
.. _tut-tuples:
348349

349350
.. _tut-tuples:
350351

Doc/tutorial/introduction.rst

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ end a multi-line command.
1313

1414
Many of the examples in this manual, even those entered at the interactive
1515
prompt, include comments. Comments in Python start with the hash character,
16-
``#``, and extend to the end of the physical line. A comment may appear at
17-
the start of a line or following whitespace or code, but not within a string
16+
``#``, and extend to the end of the physical line. A comment may appear at the
17+
start of a line or following whitespace or code, but not within a string
1818
literal. A hash character within a string literal is just a hash character.
19+
Since comments are to clarify code and are not interpreted by Python, they may
20+
be omitted when typing in examples.
1921

2022
Some examples::
2123

@@ -77,6 +79,15 @@ A value can be assigned to several variables simultaneously::
7779
>>> z
7880
0
7981

82+
Variables must be "defined" (assigned a value) before they can be used, or an
83+
error will occur::
84+
85+
>>> # try to access an undefined variable
86+
... n
87+
Traceback (most recent call last):
88+
File "<stdin>", line 1, in <module>
89+
NameError: name 'n' is not defined
90+
8091
There is full support for floating point; operators with mixed type operands
8192
convert the integer operand to floating point::
8293

@@ -269,7 +280,7 @@ omitted second index defaults to the size of the string being sliced. ::
269280
>>> word[2:] # Everything except the first two characters
270281
'lpA'
271282

272-
Unlike a C string, Python strings cannot be changed. Assigning to an indexed
283+
Unlike a C string, Python strings cannot be changed. Assigning to an indexed
273284
position in the string results in an error::
274285

275286
>>> word[0] = 'x'

0 commit comments

Comments
 (0)