summaryrefslogtreecommitdiffstats
path: root/Doc/library/functions.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/functions.rst')
-rw-r--r--Doc/library/functions.rst53
1 files changed, 34 insertions, 19 deletions
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
index 6ab85f6..3c27d82 100644
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -288,7 +288,22 @@ available. They are listed here in alphabetical order.
class's attributes, and recursively of the attributes of its class's base
classes.
- The resulting list is sorted alphabetically.
+ The resulting list is sorted alphabetically. For example:
+
+ >>> import struct
+ >>> dir() # doctest: +SKIP
+ ['__builtins__', '__doc__', '__name__', 'struct']
+ >>> dir(struct) # doctest: +NORMALIZE_WHITESPACE
+ ['Struct', '__builtins__', '__doc__', '__file__', '__name__',
+ '__package__', '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
+ 'unpack', 'unpack_from']
+ >>> class Foo(object):
+ ... def __dir__(self):
+ ... return ["kan", "ga", "roo"]
+ ...
+ >>> f = Foo()
+ >>> dir(f)
+ ['ga', 'kan', 'roo']
.. note::
@@ -318,10 +333,10 @@ available. They are listed here in alphabetical order.
iterator returned by :func:`enumerate` returns a tuple containing a count (from
zero) and the corresponding value obtained from iterating over *iterable*.
:func:`enumerate` is useful for obtaining an indexed series: ``(0, seq[0])``,
- ``(1, seq[1])``, ``(2, seq[2])``, .... For example::
+ ``(1, seq[1])``, ``(2, seq[2])``, .... For example:
>>> for i, season in enumerate(['Spring', 'Summer', 'Fall', 'Winter')]:
- >>> print(i, season)
+ ... print(i, season)
0 Spring
1 Summer
2 Fall
@@ -343,7 +358,7 @@ available. They are listed here in alphabetical order.
propagated. If the *locals* dictionary is omitted it defaults to the *globals*
dictionary. If both dictionaries are omitted, the expression is executed in the
environment where :func:`eval` is called. The return value is the result of
- the evaluated expression. Syntax errors are reported as exceptions. Example::
+ the evaluated expression. Syntax errors are reported as exceptions. Example:
>>> x = 1
>>> eval('x+1')
@@ -865,15 +880,15 @@ available. They are listed here in alphabetical order.
.. XXX does accept objects with __index__ too
.. function:: range([start,] stop[, step])
- This is a versatile function to create iterators containing arithmetic
- progressions. It is most often used in :keyword:`for` loops. The arguments
- must be integers. If the *step* argument is omitted, it defaults to ``1``.
- If the *start* argument is omitted, it defaults to ``0``. The full form
- returns an iterator of plain integers ``[start, start + step, start + 2 *
- step, ...]``. If *step* is positive, the last element is the largest ``start
- + i * step`` less than *stop*; if *step* is negative, the last element is the
- smallest ``start + i * step`` greater than *stop*. *step* must not be zero
- (or else :exc:`ValueError` is raised). Example::
+ This is a versatile function to create lists containing arithmetic progressions.
+ It is most often used in :keyword:`for` loops. The arguments must be plain
+ integers. If the *step* argument is omitted, it defaults to ``1``. If the
+ *start* argument is omitted, it defaults to ``0``. The full form returns a list
+ of plain integers ``[start, start + step, start + 2 * step, ...]``. If *step*
+ is positive, the last element is the largest ``start + i * step`` less than
+ *stop*; if *step* is negative, the last element is the smallest ``start + i *
+ step`` greater than *stop*. *step* must not be zero (or else :exc:`ValueError`
+ is raised). Example:
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@@ -1082,12 +1097,12 @@ available. They are listed here in alphabetical order.
:noindex:
Return a new type object. This is essentially a dynamic form of the
- :keyword:`class` statement. The *name* string is the class name and becomes
- the :attr:`__name__` attribute; the *bases* tuple itemizes the base classes
- and becomes the :attr:`__bases__` attribute; and the *dict* dictionary is the
- namespace containing definitions for class body and becomes the
- :attr:`__dict__` attribute. For example, the following two statements create
- identical :class:`type` objects::
+ :keyword:`class` statement. The *name* string is the class name and becomes the
+ :attr:`__name__` attribute; the *bases* tuple itemizes the base classes and
+ becomes the :attr:`__bases__` attribute; and the *dict* dictionary is the
+ namespace containing definitions for class body and becomes the :attr:`__dict__`
+ attribute. For example, the following two statements create identical
+ :class:`type` objects:
>>> class X(object):
... a = 1