@@ -56,7 +56,7 @@ Python 2.6 incorporates new features and syntax from 3.0 while
56
56
remaining compatible with existing code by not removing older features
57
57
or syntax. When it's not possible to do that, Python 2.6 tries to do
58
58
what it can, adding compatibility functions in a
59
- :mod: `future_builtins ` module and a :option: `!-3 ` switch to warn about
59
+ :mod: `! future_builtins ` module and a :option: `!-3 ` switch to warn about
60
60
usages that will become unsupported in 3.0.
61
61
62
62
Some significant new packages have been added to the standard library,
109
109
Python 3.0 adds several new built-in functions and changes the
110
110
semantics of some existing builtins. Functions that are new in 3.0
111
111
such as :func: `bin ` have simply been added to Python 2.6, but existing
112
- builtins haven't been changed; instead, the :mod: `future_builtins `
112
+ builtins haven't been changed; instead, the :mod: `! future_builtins `
113
113
module has versions with the new 3.0 semantics. Code written to be
114
114
compatible with 3.0 can do ``from future_builtins import hex, map `` as
115
115
necessary.
@@ -118,7 +118,7 @@ A new command-line switch, :option:`!-3`, enables warnings
118
118
about features that will be removed in Python 3.0. You can run code
119
119
with this switch to see how much work will be necessary to port
120
120
code to 3.0. The value of this switch is available
121
- to Python code as the boolean variable :data: `sys.py3kwarning `,
121
+ to Python code as the boolean variable :data: `! sys.py3kwarning `,
122
122
and to C extension code as :c:data: `!Py_Py3kWarningFlag `.
123
123
124
124
.. seealso ::
@@ -307,9 +307,9 @@ The :mod:`threading` module's locks and condition variables also support the
307
307
The lock is acquired before the block is executed and always released once the
308
308
block is complete.
309
309
310
- The :func: `localcontext ` function in the :mod: `decimal ` module makes it easy
311
- to save and restore the current decimal context, which encapsulates the desired
312
- precision and rounding characteristics for computations::
310
+ The :func: `~decimal. localcontext ` function in the :mod: `decimal ` module makes
311
+ it easy to save and restore the current decimal context, which encapsulates
312
+ the desired precision and rounding characteristics for computations::
313
313
314
314
from decimal import Decimal, Context, localcontext
315
315
@@ -337,12 +337,12 @@ underlying implementation and should keep reading.
337
337
A high-level explanation of the context management protocol is:
338
338
339
339
* The expression is evaluated and should result in an object called a "context
340
- manager". The context manager must have :meth: `~object.__enter__ ` and :meth: ` ~object.__exit__ `
341
- methods.
340
+ manager". The context manager must have :meth: `~object.__enter__ ` and
341
+ :meth: ` ~object.__exit__ ` methods.
342
342
343
- * The context manager's :meth: `~object.__enter__ ` method is called. The value returned
344
- is assigned to *VAR *. If no ``as VAR `` clause is present, the value is simply
345
- discarded.
343
+ * The context manager's :meth: `~object.__enter__ ` method is called. The value
344
+ returned is assigned to *VAR *. If no ``as VAR `` clause is present, the
345
+ value is simply discarded.
346
346
347
347
* The code in *BLOCK * is executed.
348
348
@@ -378,7 +378,7 @@ be to let the user write code like this::
378
378
379
379
The transaction should be committed if the code in the block runs flawlessly or
380
380
rolled back if there's an exception. Here's the basic interface for
381
- :class: `DatabaseConnection ` that I'll assume::
381
+ :class: `! DatabaseConnection ` that I'll assume::
382
382
383
383
class DatabaseConnection:
384
384
# Database interface
@@ -431,14 +431,15 @@ The contextlib module
431
431
The :mod: `contextlib ` module provides some functions and a decorator that
432
432
are useful when writing objects for use with the ':keyword: `with `' statement.
433
433
434
- The decorator is called :func: `contextmanager `, and lets you write a single
435
- generator function instead of defining a new class. The generator should yield
436
- exactly one value. The code up to the :keyword: `yield ` will be executed as the
437
- :meth: `~object.__enter__ ` method, and the value yielded will be the method's return
438
- value that will get bound to the variable in the ':keyword: `with `' statement's
439
- :keyword: `!as ` clause, if any. The code after the :keyword: `!yield ` will be
440
- executed in the :meth: `~object.__exit__ ` method. Any exception raised in the block will
441
- be raised by the :keyword: `!yield ` statement.
434
+ The decorator is called :func: `~contextlib.contextmanager `, and lets you write
435
+ a single generator function instead of defining a new class. The generator
436
+ should yield exactly one value. The code up to the :keyword: `yield ` will be
437
+ executed as the :meth: `~object.__enter__ ` method, and the value yielded will
438
+ be the method's return value that will get bound to the variable in the
439
+ ':keyword: `with `' statement's :keyword: `!as ` clause, if any. The code after
440
+ the :keyword: `!yield ` will be executed in the :meth: `~object.__exit__ ` method.
441
+ Any exception raised in the block will be raised by the :keyword: `!yield `
442
+ statement.
442
443
443
444
Using this decorator, our database example from the previous section
444
445
could be written as::
@@ -469,7 +470,7 @@ statement both starts a database transaction and acquires a thread lock::
469
470
with nested (db_transaction(db), lock) as (cursor, locked):
470
471
...
471
472
472
- Finally, the :func: `closing ` function returns its argument so that it can be
473
+ Finally, the :func: `~contextlib. closing ` function returns its argument so that it can be
473
474
bound to a variable, and calls the argument's ``.close() `` method at the end
474
475
of the block. ::
475
476
@@ -538,7 +539,7 @@ If you don't like the default directory, it can be overridden by an
538
539
environment variable. :envvar: `PYTHONUSERBASE ` sets the root
539
540
directory used for all Python versions supporting this feature. On
540
541
Windows, the directory for application-specific data can be changed by
541
- setting the :envvar: `APPDATA ` environment variable. You can also
542
+ setting the :envvar: `! APPDATA ` environment variable. You can also
542
543
modify the :file: `site.py ` file for your Python installation.
543
544
544
545
The feature can be disabled entirely by running Python with the
@@ -568,11 +569,12 @@ The :mod:`multiprocessing` module started out as an exact emulation of
568
569
the :mod: `threading ` module using processes instead of threads. That
569
570
goal was discarded along the path to Python 2.6, but the general
570
571
approach of the module is still similar. The fundamental class
571
- is the :class: `Process `, which is passed a callable object and
572
- a collection of arguments. The :meth: `start ` method
572
+ is the :class: `~multiprocessing. Process `, which is passed a callable object and
573
+ a collection of arguments. The :meth: `~multiprocessing.Process. start ` method
573
574
sets the callable running in a subprocess, after which you can call
574
- the :meth: `is_alive ` method to check whether the subprocess is still running
575
- and the :meth: `join ` method to wait for the process to exit.
575
+ the :meth: `~multiprocessing.Process.is_alive ` method to check whether the
576
+ subprocess is still running and the :meth: `~multiprocessing.Process.join `
577
+ method to wait for the process to exit.
576
578
577
579
Here's a simple example where the subprocess will calculate a
578
580
factorial. The function doing the calculation is written strangely so
@@ -619,13 +621,16 @@ the object to communicate. (If the parent were to change the value of
619
621
the global variable, the child's value would be unaffected, and vice
620
622
versa.)
621
623
622
- Two other classes, :class: `Pool ` and :class: `Manager `, provide
623
- higher-level interfaces. :class: `Pool ` will create a fixed number of
624
- worker processes, and requests can then be distributed to the workers
625
- by calling :meth: `apply ` or :meth: `apply_async ` to add a single request,
626
- and :meth: `map ` or :meth: `map_async ` to add a number of
627
- requests. The following code uses a :class: `Pool ` to spread requests
628
- across 5 worker processes and retrieve a list of results::
624
+ Two other classes, :class: `~multiprocessing.pool.Pool ` and
625
+ :class: `~multiprocessing.Manager `, provide higher-level interfaces.
626
+ :class: `~multiprocessing.pool.Pool ` will create a fixed number of worker
627
+ processes, and requests can then be distributed to the workers by calling
628
+ :meth: `~multiprocessing.pool.Pool.apply ` or
629
+ :meth: `~multiprocessing.pool.Pool.apply_async ` to add a single request, and
630
+ :meth: `~multiprocessing.pool.Pool.map ` or
631
+ :meth: `~multiprocessing.pool.Pool.map_async ` to add a number of
632
+ requests. The following code uses a :class: `~multiprocessing.pool.Pool ` to
633
+ spread requests across 5 worker processes and retrieve a list of results::
629
634
630
635
from multiprocessing import Pool
631
636
@@ -646,15 +651,18 @@ This produces the following output::
646
651
33452526613163807108170062053440751665152000000000
647
652
...
648
653
649
- The other high-level interface, the :class: `Manager ` class, creates a
650
- separate server process that can hold master copies of Python data
654
+ The other high-level interface, the :class: `~multiprocessing. Manager ` class,
655
+ creates a separate server process that can hold master copies of Python data
651
656
structures. Other processes can then access and modify these data
652
657
structures using proxy objects. The following example creates a
653
658
shared dictionary by calling the :meth: `dict ` method; the worker
654
659
processes then insert values into the dictionary. (Locking is not
655
660
done for you automatically, which doesn't matter in this example.
656
- :class: `Manager `'s methods also include :meth: `Lock `, :meth: `RLock `,
657
- and :meth: `Semaphore ` to create shared locks.)
661
+ :class: `~multiprocessing.Manager `'s methods also include
662
+ :meth: `~multiprocessing.managers.SyncManager.Lock `,
663
+ :meth: `~multiprocessing.managers.SyncManager.RLock `,
664
+ and :meth: `~multiprocessing.managers.SyncManager.Semaphore ` to create
665
+ shared locks.)
658
666
659
667
::
660
668
@@ -824,7 +832,7 @@ documentation for a :ref:`complete list <formatstrings>`; here's a sample:
824
832
format, followed by a percent sign.
825
833
===== ========================================================================
826
834
827
- Classes and types can define a :meth: `__format__ ` method to control how they're
835
+ Classes and types can define a :meth: `~object. __format__ ` method to control how they're
828
836
formatted. It receives a single argument, the format specifier::
829
837
830
838
def __format__(self, format_spec):
@@ -834,7 +842,7 @@ formatted. It receives a single argument, the format specifier::
834
842
return str(self)
835
843
836
844
There's also a :func: `format ` builtin that will format a single
837
- value. It calls the type's :meth: `__format__ ` method with the
845
+ value. It calls the type's :meth: `~object. __format__ ` method with the
838
846
provided specifier::
839
847
840
848
>>> format(75.6564, '.2f')
@@ -1029,56 +1037,58 @@ PEP 3116: New I/O Library
1029
1037
1030
1038
Python's built-in file objects support a number of methods, but
1031
1039
file-like objects don't necessarily support all of them. Objects that
1032
- imitate files usually support :meth: `read ` and :meth: ` write `, but they
1033
- may not support :meth: `readline `, for example. Python 3.0 introduces
1034
- a layered I/O library in the :mod: `io ` module that separates buffering
1035
- and text-handling features from the fundamental read and write
1036
- operations.
1040
+ imitate files usually support :meth: `! read ` and
1041
+ :meth: ` !write `, but they may not support :meth: `! readline `,
1042
+ for example. Python 3.0 introduces a layered I/O library in the :mod: `io `
1043
+ module that separates buffering and text-handling features from the
1044
+ fundamental read and write operations.
1037
1045
1038
1046
There are three levels of abstract base classes provided by
1039
1047
the :mod: `io ` module:
1040
1048
1041
- * :class: `RawIOBase ` defines raw I/O operations: :meth: `read `,
1042
- :meth: `readinto `,
1043
- :meth: `write `, :meth: ` seek `, :meth: `tell `, :meth: `truncate `,
1044
- and :meth: `close `.
1049
+ * :class: `~io. RawIOBase ` defines raw I/O operations: :meth: `~io.RawIOBase. read `,
1050
+ :meth: `~io.RawIOBase. readinto`, :meth: ` ~io.RawIOBase.write `,
1051
+ :meth: `~io.IOBase. seek `, :meth: `~io.IOBase. tell `, :meth: `~io.IOBase. truncate `,
1052
+ and :meth: `~io.IOBase. close `.
1045
1053
Most of the methods of this class will often map to a single system call.
1046
- There are also :meth: `readable `, :meth: `writable `, and :meth: `seekable `
1047
- methods for determining what operations a given object will allow.
1054
+ There are also :meth: `~io.IOBase.readable `, :meth: `~io.IOBase.writable `,
1055
+ and :meth: `~io.IOBase.seekable ` methods for determining what operations a
1056
+ given object will allow.
1048
1057
1049
1058
Python 3.0 has concrete implementations of this class for files and
1050
1059
sockets, but Python 2.6 hasn't restructured its file and socket objects
1051
1060
in this way.
1052
1061
1053
- * :class: `BufferedIOBase ` is an abstract base class that
1062
+ * :class: `~io. BufferedIOBase ` is an abstract base class that
1054
1063
buffers data in memory to reduce the number of
1055
1064
system calls used, making I/O processing more efficient.
1056
- It supports all of the methods of :class: `RawIOBase `,
1057
- and adds a :attr: `raw ` attribute holding the underlying raw object.
1065
+ It supports all of the methods of :class: `~io.RawIOBase `,
1066
+ and adds a :attr: `~io.BufferedIOBase.raw ` attribute holding the underlying
1067
+ raw object.
1058
1068
1059
1069
There are five concrete classes implementing this ABC.
1060
- :class: `BufferedWriter ` and :class: `BufferedReader ` are for objects
1061
- that support write-only or read-only usage that have a :meth: `seek `
1062
- method for random access. :class: `BufferedRandom ` objects support
1070
+ :class: `~io. BufferedWriter ` and :class: `~io. BufferedReader ` are for objects
1071
+ that support write-only or read-only usage that have a :meth: `~io.IOBase. seek `
1072
+ method for random access. :class: `~io. BufferedRandom ` objects support
1063
1073
read and write access upon the same underlying stream, and
1064
- :class: `BufferedRWPair ` is for objects such as TTYs that have both
1074
+ :class: `~io. BufferedRWPair ` is for objects such as TTYs that have both
1065
1075
read and write operations acting upon unconnected streams of data.
1066
- The :class: `BytesIO ` class supports reading, writing, and seeking
1076
+ The :class: `~io. BytesIO ` class supports reading, writing, and seeking
1067
1077
over an in-memory buffer.
1068
1078
1069
1079
.. index ::
1070
1080
single: universal newlines; What's new
1071
1081
1072
- * :class: `TextIOBase `: Provides functions for reading and writing
1082
+ * :class: `~io. TextIOBase `: Provides functions for reading and writing
1073
1083
strings (remember, strings will be Unicode in Python 3.0),
1074
- and supporting :term: `universal newlines `. :class: `TextIOBase ` defines
1084
+ and supporting :term: `universal newlines `. :class: `~io. TextIOBase ` defines
1075
1085
the :meth: `readline ` method and supports iteration upon
1076
1086
objects.
1077
1087
1078
- There are two concrete implementations. :class: `TextIOWrapper `
1088
+ There are two concrete implementations. :class: `~io. TextIOWrapper `
1079
1089
wraps a buffered I/O object, supporting all of the methods for
1080
- text I/O and adding a :attr: `buffer ` attribute for access
1081
- to the underlying object. :class: `StringIO ` simply buffers
1090
+ text I/O and adding a :attr: `~io.TextIOBase. buffer ` attribute for access
1091
+ to the underlying object. :class: `~io. StringIO ` simply buffers
1082
1092
everything in memory without ever writing anything to disk.
1083
1093
1084
1094
(In Python 2.6, :class: `io.StringIO ` is implemented in
@@ -1162,7 +1172,7 @@ Some object-oriented languages such as Java support interfaces,
1162
1172
declaring that a class has a given set of methods or supports a given
1163
1173
access protocol. Abstract Base Classes (or ABCs) are an equivalent
1164
1174
feature for Python. The ABC support consists of an :mod: `abc ` module
1165
- containing a metaclass called :class: `ABCMeta `, special handling of
1175
+ containing a metaclass called :class: `~abc. ABCMeta `, special handling of
1166
1176
this metaclass by the :func: `isinstance ` and :func: `issubclass `
1167
1177
builtins, and a collection of basic ABCs that the Python developers
1168
1178
think will be widely useful. Future versions of Python will probably
@@ -1172,17 +1182,17 @@ Let's say you have a particular class and wish to know whether it supports
1172
1182
dictionary-style access. The phrase "dictionary-style" is vague, however.
1173
1183
It probably means that accessing items with ``obj[1] `` works.
1174
1184
Does it imply that setting items with ``obj[2] = value `` works?
1175
- Or that the object will have :meth: `keys `, :meth: `values `, and :meth: `items `
1176
- methods? What about the iterative variants such as :meth: `iterkeys `? :meth: ` copy `
1177
- and :meth: `update `? Iterating over the object with :func: `iter `?
1185
+ Or that the object will have :meth: `! keys `, :meth: `! values `, and :meth: `! items `
1186
+ methods? What about the iterative variants such as :meth: `! iterkeys `?
1187
+ :meth: ` !copy` and :meth: `! update`? Iterating over the object with :func: `! iter `?
1178
1188
1179
1189
The Python 2.6 :mod: `collections ` module includes a number of
1180
1190
different ABCs that represent these distinctions. :class: `Iterable `
1181
- indicates that a class defines :meth: `__iter__ `, and
1182
- :class: `Container ` means the class defines a :meth: `__contains__ `
1191
+ indicates that a class defines :meth: `~object. __iter__ `, and
1192
+ :class: `Container ` means the class defines a :meth: `~object. __contains__ `
1183
1193
method and therefore supports ``x in y `` expressions. The basic
1184
1194
dictionary interface of getting items, setting items, and
1185
- :meth: `keys `, :meth: `values `, and :meth: `items `, is defined by the
1195
+ :meth: `! keys `, :meth: `! values `, and :meth: `! items `, is defined by the
1186
1196
:class: `MutableMapping ` ABC.
1187
1197
1188
1198
You can derive your own classes from a particular ABC
@@ -1196,7 +1206,7 @@ to indicate they support that ABC's interface::
1196
1206
1197
1207
Alternatively, you could write the class without deriving from
1198
1208
the desired ABC and instead register the class by
1199
- calling the ABC's :meth: `register ` method::
1209
+ calling the ABC's :meth: `~abc.ABCMeta. register ` method::
1200
1210
1201
1211
import collections
1202
1212
@@ -1206,10 +1216,10 @@ calling the ABC's :meth:`register` method::
1206
1216
collections.MutableMapping.register(Storage)
1207
1217
1208
1218
For classes that you write, deriving from the ABC is probably clearer.
1209
- The :meth: `register ` method is useful when you've written a new
1219
+ The :meth: `~abc.ABCMeta. register ` method is useful when you've written a new
1210
1220
ABC that can describe an existing type or class, or if you want
1211
1221
to declare that some third-party class implements an ABC.
1212
- For example, if you defined a :class: `PrintableType ` ABC,
1222
+ For example, if you defined a :class: `! PrintableType ` ABC,
1213
1223
it's legal to do::
1214
1224
1215
1225
# Register Python's types
@@ -1256,16 +1266,16 @@ metaclass in a class definition::
1256
1266
...
1257
1267
1258
1268
1259
- In the :class: `Drawable ` ABC above, the :meth: `draw_doubled ` method
1269
+ In the :class: `! Drawable ` ABC above, the :meth: `! draw_doubled ` method
1260
1270
renders the object at twice its size and can be implemented in terms
1261
- of other methods described in :class: `Drawable `. Classes implementing
1271
+ of other methods described in :class: `! Drawable `. Classes implementing
1262
1272
this ABC therefore don't need to provide their own implementation
1263
- of :meth: `draw_doubled `, though they can do so. An implementation
1264
- of :meth: `draw ` is necessary, though; the ABC can't provide
1273
+ of :meth: `! draw_doubled `, though they can do so. An implementation
1274
+ of :meth: `! draw ` is necessary, though; the ABC can't provide
1265
1275
a useful generic implementation.
1266
1276
1267
- You can apply the `` @ abstractmethod` ` decorator to methods such as
1268
- :meth: `draw ` that must be implemented; Python will then raise an
1277
+ You can apply the :deco: ` ~abc. abstractmethod ` decorator to methods such as
1278
+ :meth: `! draw ` that must be implemented; Python will then raise an
1269
1279
exception for classes that don't define the method.
1270
1280
Note that the exception is only raised when you actually
1271
1281
try to create an instance of a subclass lacking the method::
@@ -1289,7 +1299,7 @@ Abstract data attributes can be declared using the
1289
1299
def readonly(self):
1290
1300
return self._x
1291
1301
1292
- Subclasses must then define a :meth: ` readonly ` property.
1302
+ Subclasses must then define a `` readonly ` ` property.
1293
1303
1294
1304
.. seealso ::
1295
1305
@@ -2739,13 +2749,13 @@ numbers.
2739
2749
2740
2750
.. ======================================================================
2741
2751
2742
- The :mod: `future_builtins ` module
2752
+ The :mod: `! future_builtins ` module
2743
2753
--------------------------------------
2744
2754
2745
2755
Python 3.0 makes many changes to the repertoire of built-in
2746
2756
functions, and most of the changes can't be introduced in the Python
2747
2757
2.x series because they would break compatibility.
2748
- The :mod: `future_builtins ` module provides versions
2758
+ The :mod: `! future_builtins ` module provides versions
2749
2759
of these built-in functions that can be imported when writing
2750
2760
3.0-compatible code.
2751
2761
0 commit comments