Skip to content

Commit 690d4ae

Browse files
committed
Multiple clean-ups to the docs for builtin functions.
* Use concrete example for dir() and eliminate the distracting doctest directives. * Add a pure python equivalent for enumerate() * Modify the enumerate() example to demonstrate the start argument * Remove incorrect reference the *iterable* in the enumerate() docs. * Downgrade the comments on input() from a warning to a note. * Fix the iter() example to use the empty string as the terminating condition for file.readline(). Also, the old example was broken because readline() results include a newline, so 'STOP\n' would have been the correct terminating condition. Even with that fix, the STOP example was fragile and would have lead to infinite loops with malformed inputs. * Do not refer to classmethod as being "more advanced" than staticmethod.
1 parent 783a30f commit 690d4ae

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

Doc/library/functions.rst

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -298,19 +298,19 @@ available. They are listed here in alphabetical order.
298298
The resulting list is sorted alphabetically. For example:
299299

300300
>>> import struct
301-
>>> dir() # doctest: +SKIP
301+
>>> dir() # show the names in the module namespace
302302
['__builtins__', '__doc__', '__name__', 'struct']
303-
>>> dir(struct) # doctest: +NORMALIZE_WHITESPACE
303+
>>> dir(struct) # show the names in the struct module
304304
['Struct', '__builtins__', '__doc__', '__file__', '__name__',
305305
'__package__', '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
306306
'unpack', 'unpack_from']
307-
>>> class Foo(object):
308-
... def __dir__(self):
309-
... return ["kan", "ga", "roo"]
310-
...
311-
>>> f = Foo()
307+
>>> class Shape(object):
308+
def __dir__(self):
309+
return ['area', 'perimter', 'location']
310+
311+
>>> f = Shape()
312312
>>> dir(f)
313-
['ga', 'kan', 'roo']
313+
['area', 'perimter', 'location']
314314

315315
.. note::
316316

@@ -342,16 +342,22 @@ available. They are listed here in alphabetical order.
342342
:term:`iterator`, or some other object which supports iteration. The
343343
:meth:`!next` method of the iterator returned by :func:`enumerate` returns a
344344
tuple containing a count (from *start* which defaults to 0) and the
345-
corresponding value obtained from iterating over *iterable*.
346-
:func:`enumerate` is useful for obtaining an indexed series: ``(0, seq[0])``,
347-
``(1, seq[1])``, ``(2, seq[2])``, .... For example:
345+
corresponding value obtained from iterating over *sequence*::
346+
347+
>>> for i, season in enumerate('Spring Summer Fall Winter'.split(), start=1):
348+
print i, season
349+
1 Spring
350+
2 Summer
351+
3 Fall
352+
4 Winter
348353

349-
>>> for i, season in enumerate(['Spring', 'Summer', 'Fall', 'Winter']):
350-
... print i, season
351-
0 Spring
352-
1 Summer
353-
2 Fall
354-
3 Winter
354+
Equivalent to::
355+
356+
def enumerate(sequence, start=0):
357+
n = start
358+
for elem in sequence:
359+
yield n, elem
360+
n += 1
355361

356362
.. versionadded:: 2.3
357363
.. versionadded:: 2.6
@@ -586,13 +592,12 @@ available. They are listed here in alphabetical order.
586592

587593
Equivalent to ``eval(raw_input(prompt))``.
588594

589-
.. warning::
595+
.. note::
590596

591-
This function is not safe from user errors! It expects a valid Python
592-
expression as input; if the input is not syntactically valid, a
593-
:exc:`SyntaxError` will be raised. Other exceptions may be raised if there is an
594-
error during evaluation. (On the other hand, sometimes this is exactly what you
595-
need when writing a quick script for expert use.)
597+
This function does not catch user errors. It expects a valid Python
598+
expression as input. If the input is not syntactically valid, a
599+
:exc:`SyntaxError` will be raised. Other exceptions may be raised if there
600+
is an error during evaluation.
596601

597602
If the :mod:`readline` module was loaded, then :func:`input` will use it to
598603
provide elaborate line editing and history features.
@@ -660,10 +665,10 @@ available. They are listed here in alphabetical order.
660665

661666
One useful application of the second form of :func:`iter` is to read lines of
662667
a file until a certain line is reached. The following example reads a file
663-
until ``"STOP"`` is reached: ::
668+
until the :meth:`readline` method returns an empty string::
664669

665-
with open("mydata.txt") as fp:
666-
for line in iter(fp.readline, "STOP"):
670+
with open('mydata.txt') as fp:
671+
for line in iter(fp.readline, ''):
667672
process_line(line)
668673

669674
.. versionadded:: 2.2
@@ -1241,8 +1246,9 @@ available. They are listed here in alphabetical order.
12411246
It can be called either on the class (such as ``C.f()``) or on an instance (such
12421247
as ``C().f()``). The instance is ignored except for its class.
12431248

1244-
Static methods in Python are similar to those found in Java or C++. For a more
1245-
advanced concept, see :func:`classmethod` in this section.
1249+
Static methods in Python are similar to those found in Java or C++. Also see
1250+
:func:`classmethod` for a variant that is useful for creating alternate
1251+
class constructors.
12461252

12471253
For more information on static methods, consult the documentation on the
12481254
standard type hierarchy in :ref:`types`.

0 commit comments

Comments
 (0)