@@ -298,19 +298,19 @@ available. They are listed here in alphabetical order.
298
298
The resulting list is sorted alphabetically. For example:
299
299
300
300
>>> import struct
301
- >>> dir () # doctest: +SKIP
301
+ >>> dir () # show the names in the module namespace
302
302
['__builtins__', '__doc__', '__name__', 'struct']
303
- >>> dir (struct) # doctest: +NORMALIZE_WHITESPACE
303
+ >>> dir (struct) # show the names in the struct module
304
304
['Struct', '__builtins__', '__doc__', '__file__', '__name__',
305
305
'__package__', '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
306
306
'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 ()
312
312
>>> dir (f)
313
- ['ga ', 'kan ', 'roo ']
313
+ ['area ', 'perimter ', 'location ']
314
314
315
315
.. note ::
316
316
@@ -342,16 +342,22 @@ available. They are listed here in alphabetical order.
342
342
:term: `iterator `, or some other object which supports iteration. The
343
343
:meth: `!next ` method of the iterator returned by :func: `enumerate ` returns a
344
344
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
348
353
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
355
361
356
362
.. versionadded :: 2.3
357
363
.. versionadded :: 2.6
@@ -586,13 +592,12 @@ available. They are listed here in alphabetical order.
586
592
587
593
Equivalent to ``eval(raw_input(prompt)) ``.
588
594
589
- .. warning ::
595
+ .. note ::
590
596
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.
596
601
597
602
If the :mod: `readline ` module was loaded, then :func: `input ` will use it to
598
603
provide elaborate line editing and history features.
@@ -660,10 +665,10 @@ available. They are listed here in alphabetical order.
660
665
661
666
One useful application of the second form of :func: `iter ` is to read lines of
662
667
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 ::
664
669
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, '' ):
667
672
process_line(line)
668
673
669
674
.. versionadded :: 2.2
@@ -1241,8 +1246,9 @@ available. They are listed here in alphabetical order.
1241
1246
It can be called either on the class (such as ``C.f() ``) or on an instance (such
1242
1247
as ``C().f() ``). The instance is ignored except for its class.
1243
1248
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.
1246
1252
1247
1253
For more information on static methods, consult the documentation on the
1248
1254
standard type hierarchy in :ref: `types `.
0 commit comments