diff options
Diffstat (limited to 'Doc/tutorial')
-rw-r--r-- | Doc/tutorial/classes.rst | 12 | ||||
-rw-r--r-- | Doc/tutorial/inputoutput.rst | 15 |
2 files changed, 18 insertions, 9 deletions
diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 7ef4153..4e166d1 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -51,8 +51,8 @@ need for two different argument passing mechanisms as in Pascal. .. _tut-scopes: -Python Scopes and Name Spaces -============================= +Python Scopes and Namespaces +============================ Before introducing classes, I first have to tell you something about Python's scope rules. Class definitions play some neat tricks with namespaces, and you @@ -87,7 +87,7 @@ attributes is possible. Module attributes are writable: you can write :keyword:`del` statement. For example, ``del modname.the_answer`` will remove the attribute :attr:`the_answer` from the object named by ``modname``. -Name spaces are created at different moments and have different lifetimes. The +Namespaces are created at different moments and have different lifetimes. The namespace containing the built-in names is created when the Python interpreter starts up, and is never deleted. The global namespace for a module is created when the module definition is read in; normally, module namespaces also last @@ -381,9 +381,9 @@ data attribute, its class is searched. If the name denotes a valid class attribute that is a function object, a method object is created by packing (pointers to) the instance object and the function object just found together in an abstract object: this is the method object. When the method object is called -with an argument list, it is unpacked again, a new argument list is constructed -from the instance object and the original argument list, and the function object -is called with this new argument list. +with an argument list, a new argument list is constructed from the instance +object and the argument list, and the function object is called with this new +argument list. .. _tut-remarks: diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst index 549a922..dbb56f6 100644 --- a/Doc/tutorial/inputoutput.rst +++ b/Doc/tutorial/inputoutput.rst @@ -126,12 +126,12 @@ with zeros. It understands about plus and minus signs:: Basic usage of the :meth:`str.format` method looks like this:: - >>> print('We are the {0} who say "{1}!"'.format('knights', 'Ni')) + >>> print('We are the {} who say "{}!"'.format('knights', 'Ni')) We are the knights who say "Ni!" The brackets and characters within them (called format fields) are replaced with -the objects passed into the :meth:`~str.format` method. The number in the -brackets refers to the position of the object passed into the +the objects passed into the :meth:`~str.format` method. A number in the +brackets can be used to refer to the position of the object passed into the :meth:`~str.format` method. :: >>> print('{0} and {1}'.format('spam', 'eggs')) @@ -152,6 +152,15 @@ Positional and keyword arguments can be arbitrarily combined:: other='Georg')) The story of Bill, Manfred, and Georg. +``'!a'`` (apply :func:`ascii`), ``'!s'`` (apply :func:`str`) and ``'!r'`` +(apply :func:`repr`) can be used to convert the value before it is formatted:: + + >>> import math + >>> print('The value of PI is approximately {}.'.format(math.pi)) + The value of PI is approximately 3.14159265359. + >>> print('The value of PI is approximately {!r}.'.format(math.pi)) + The value of PI is approximately 3.141592653589793. + An optional ``':'`` and format specifier can follow the field name. This allows greater control over how the value is formatted. The following example truncates Pi to three places after the decimal. |