mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
#4058: fix some whatsnew markup.
This commit is contained in:
parent
26497d91ca
commit
06a1386902
1 changed files with 172 additions and 172 deletions
|
@ -723,31 +723,32 @@ In 2.6, both 8-bit and Unicode strings have a `.format()` method that
|
||||||
treats the string as a template and takes the arguments to be formatted.
|
treats the string as a template and takes the arguments to be formatted.
|
||||||
The formatting template uses curly brackets (`{`, `}`) as special characters::
|
The formatting template uses curly brackets (`{`, `}`) as special characters::
|
||||||
|
|
||||||
# Substitute positional argument 0 into the string.
|
>>> # Substitute positional argument 0 into the string.
|
||||||
"User ID: {0}".format("root") -> "User ID: root"
|
>>> "User ID: {0}".format("root")
|
||||||
|
'User ID: root'
|
||||||
# Use the named keyword arguments
|
>>> # Use the named keyword arguments
|
||||||
'User ID: {uid} Last seen: {last_login}'.format(
|
>>> "User ID: {uid} Last seen: {last_login}".format(
|
||||||
uid='root',
|
... uid="root",
|
||||||
last_login = '5 Mar 2008 07:20') ->
|
... last_login = "5 Mar 2008 07:20")
|
||||||
'User ID: root Last seen: 5 Mar 2008 07:20'
|
'User ID: root Last seen: 5 Mar 2008 07:20'
|
||||||
|
|
||||||
Curly brackets can be escaped by doubling them::
|
Curly brackets can be escaped by doubling them::
|
||||||
|
|
||||||
format("Empty dict: {{}}") -> "Empty dict: {}"
|
>>> format("Empty dict: {{}}")
|
||||||
|
"Empty dict: {}"
|
||||||
|
|
||||||
Field names can be integers indicating positional arguments, such as
|
Field names can be integers indicating positional arguments, such as
|
||||||
``{0}``, ``{1}``, etc. or names of keyword arguments. You can also
|
``{0}``, ``{1}``, etc. or names of keyword arguments. You can also
|
||||||
supply compound field names that read attributes or access dictionary keys::
|
supply compound field names that read attributes or access dictionary keys::
|
||||||
|
|
||||||
import sys
|
>>> import sys
|
||||||
'Platform: {0.platform}\nPython version: {0.version}'.format(sys) ->
|
>>> print 'Platform: {0.platform}\nPython version: {0.version}'.format(sys)
|
||||||
'Platform: darwin\n
|
Platform: darwin
|
||||||
Python version: 2.6a1+ (trunk:61261M, Mar 5 2008, 20:29:41) \n
|
Python version: 2.6a1+ (trunk:61261M, Mar 5 2008, 20:29:41)
|
||||||
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]'
|
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]'
|
||||||
|
|
||||||
import mimetypes
|
>>> import mimetypes
|
||||||
'Content-type: {0[.mp4]}'.format(mimetypes.types_map) ->
|
>>> 'Content-type: {0[.mp4]}'.format(mimetypes.types_map)
|
||||||
'Content-type: video/mp4'
|
'Content-type: video/mp4'
|
||||||
|
|
||||||
Note that when using dictionary-style notation such as ``[.mp4]``, you
|
Note that when using dictionary-style notation such as ``[.mp4]``, you
|
||||||
|
@ -760,29 +761,24 @@ So far we've shown how to specify which field to substitute into the
|
||||||
resulting string. The precise formatting used is also controllable by
|
resulting string. The precise formatting used is also controllable by
|
||||||
adding a colon followed by a format specifier. For example::
|
adding a colon followed by a format specifier. For example::
|
||||||
|
|
||||||
# Field 0: left justify, pad to 15 characters
|
>>> # Field 0: left justify, pad to 15 characters
|
||||||
# Field 1: right justify, pad to 6 characters
|
>>> # Field 1: right justify, pad to 6 characters
|
||||||
fmt = '{0:15} ${1:>6}'
|
>>> fmt = '{0:15} ${1:>6}'
|
||||||
|
>>> fmt.format('Registration', 35)
|
||||||
fmt.format('Registration', 35) ->
|
|
||||||
'Registration $ 35'
|
'Registration $ 35'
|
||||||
|
>>> fmt.format('Tutorial', 50)
|
||||||
fmt.format('Tutorial', 50) ->
|
|
||||||
'Tutorial $ 50'
|
'Tutorial $ 50'
|
||||||
|
>>> fmt.format('Banquet', 125)
|
||||||
fmt.format('Banquet', 125) ->
|
|
||||||
'Banquet $ 125'
|
'Banquet $ 125'
|
||||||
|
|
||||||
Format specifiers can reference other fields through nesting::
|
Format specifiers can reference other fields through nesting::
|
||||||
|
|
||||||
fmt = '{0:{1}}'
|
>>> fmt = '{0:{1}}'
|
||||||
|
>>> width = 15
|
||||||
width = 15
|
>>> fmt.format('Invoice #1234', width)
|
||||||
fmt.format('Invoice #1234', width) ->
|
|
||||||
'Invoice #1234 '
|
'Invoice #1234 '
|
||||||
|
>>> width = 35
|
||||||
width = 35
|
>>> fmt.format('Invoice #1234', width)
|
||||||
fmt.format('Invoice #1234', width) ->
|
|
||||||
'Invoice #1234 '
|
'Invoice #1234 '
|
||||||
|
|
||||||
The alignment of a field within the desired width can be specified:
|
The alignment of a field within the desired width can be specified:
|
||||||
|
@ -798,7 +794,7 @@ Character Effect
|
||||||
|
|
||||||
Format specifiers can also include a presentation type, which
|
Format specifiers can also include a presentation type, which
|
||||||
controls how the value is formatted. For example, floating-point numbers
|
controls how the value is formatted. For example, floating-point numbers
|
||||||
can be formatted as a general number or in exponential notation:
|
can be formatted as a general number or in exponential notation::
|
||||||
|
|
||||||
>>> '{0:g}'.format(3.75)
|
>>> '{0:g}'.format(3.75)
|
||||||
'3.75'
|
'3.75'
|
||||||
|
@ -806,25 +802,27 @@ can be formatted as a general number or in exponential notation:
|
||||||
'3.750000e+00'
|
'3.750000e+00'
|
||||||
|
|
||||||
A variety of presentation types are available. Consult the 2.6
|
A variety of presentation types are available. Consult the 2.6
|
||||||
documentation for a :ref:`complete list <formatstrings>`; here's a sample::
|
documentation for a :ref:`complete list <formatstrings>`; here's a sample:
|
||||||
|
|
||||||
'b' - Binary. Outputs the number in base 2.
|
===== ========================================================================
|
||||||
'c' - Character. Converts the integer to the corresponding
|
``b`` Binary. Outputs the number in base 2.
|
||||||
Unicode character before printing.
|
``c`` Character. Converts the integer to the corresponding Unicode character
|
||||||
'd' - Decimal Integer. Outputs the number in base 10.
|
before printing.
|
||||||
'o' - Octal format. Outputs the number in base 8.
|
``d`` Decimal Integer. Outputs the number in base 10.
|
||||||
'x' - Hex format. Outputs the number in base 16, using lower-
|
``o`` Octal format. Outputs the number in base 8.
|
||||||
case letters for the digits above 9.
|
``x`` Hex format. Outputs the number in base 16, using lower-case letters for
|
||||||
'e' - Exponent notation. Prints the number in scientific
|
the digits above 9.
|
||||||
notation using the letter 'e' to indicate the exponent.
|
``e`` Exponent notation. Prints the number in scientific notation using the
|
||||||
'g' - General format. This prints the number as a fixed-point
|
letter 'e' to indicate the exponent.
|
||||||
number, unless the number is too large, in which case
|
``g`` General format. This prints the number as a fixed-point number, unless
|
||||||
it switches to 'e' exponent notation.
|
the number is too large, in which case it switches to 'e' exponent
|
||||||
'n' - Number. This is the same as 'g' (for floats) or 'd' (for
|
notation.
|
||||||
integers), except that it uses the current locale setting to
|
``n`` Number. This is the same as 'g' (for floats) or 'd' (for integers),
|
||||||
insert the appropriate number separator characters.
|
except that it uses the current locale setting to insert the appropriate
|
||||||
'%' - Percentage. Multiplies the number by 100 and displays
|
number separator characters.
|
||||||
in fixed ('f') format, followed by a percent sign.
|
``%`` Percentage. Multiplies the number by 100 and displays in fixed ('f')
|
||||||
|
format, followed by a percent sign.
|
||||||
|
===== ========================================================================
|
||||||
|
|
||||||
Classes and types can define a :meth:`__format__` method to control how they're
|
Classes and types can define a :meth:`__format__` method to control how they're
|
||||||
formatted. It receives a single argument, the format specifier::
|
formatted. It receives a single argument, the format specifier::
|
||||||
|
@ -865,13 +863,14 @@ by doing ``def print(...)`` or importing a new function from somewhere else.
|
||||||
Python 2.6 has a ``__future__`` import that removes ``print`` as language
|
Python 2.6 has a ``__future__`` import that removes ``print`` as language
|
||||||
syntax, letting you use the functional form instead. For example::
|
syntax, letting you use the functional form instead. For example::
|
||||||
|
|
||||||
from __future__ import print_function
|
>>> from __future__ import print_function
|
||||||
print('# of entries', len(dictionary), file=sys.stderr)
|
>>> print('# of entries', len(dictionary), file=sys.stderr)
|
||||||
|
|
||||||
The signature of the new function is::
|
The signature of the new function is::
|
||||||
|
|
||||||
def print(*args, sep=' ', end='\n', file=None)
|
def print(*args, sep=' ', end='\n', file=None)
|
||||||
|
|
||||||
|
|
||||||
The parameters are:
|
The parameters are:
|
||||||
|
|
||||||
* *args*: positional arguments whose values will be printed out.
|
* *args*: positional arguments whose values will be printed out.
|
||||||
|
@ -1002,6 +1001,8 @@ Byte arrays support most of the methods of string types, such as
|
||||||
and some of the methods of lists, such as :meth:`append`,
|
and some of the methods of lists, such as :meth:`append`,
|
||||||
:meth:`pop`, and :meth:`reverse`.
|
:meth:`pop`, and :meth:`reverse`.
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
>>> b = bytearray('ABC')
|
>>> b = bytearray('ABC')
|
||||||
>>> b.append('d')
|
>>> b.append('d')
|
||||||
>>> b.append(ord('e'))
|
>>> b.append(ord('e'))
|
||||||
|
@ -1331,7 +1332,7 @@ built-in returns the binary representation for a number::
|
||||||
The :func:`int` and :func:`long` built-ins will now accept the "0o"
|
The :func:`int` and :func:`long` built-ins will now accept the "0o"
|
||||||
and "0b" prefixes when base-8 or base-2 are requested, or when the
|
and "0b" prefixes when base-8 or base-2 are requested, or when the
|
||||||
*base* argument is zero (signalling that the base used should be
|
*base* argument is zero (signalling that the base used should be
|
||||||
determined from the string):
|
determined from the string)::
|
||||||
|
|
||||||
>>> int ('0o52', 0)
|
>>> int ('0o52', 0)
|
||||||
42
|
42
|
||||||
|
@ -1504,7 +1505,7 @@ Some smaller changes made to the core Python language are:
|
||||||
(Contributed by Alexander Belopolsky; :issue:`1686487`.)
|
(Contributed by Alexander Belopolsky; :issue:`1686487`.)
|
||||||
|
|
||||||
It's also become legal to provide keyword arguments after a ``*args`` argument
|
It's also become legal to provide keyword arguments after a ``*args`` argument
|
||||||
to a function call.
|
to a function call. ::
|
||||||
|
|
||||||
>>> def f(*args, **kw):
|
>>> def f(*args, **kw):
|
||||||
... print args, kw
|
... print args, kw
|
||||||
|
@ -1878,8 +1879,8 @@ changes, or look through the Subversion logs for all the details.
|
||||||
|
|
||||||
>>> var_type = collections.namedtuple('variable',
|
>>> var_type = collections.namedtuple('variable',
|
||||||
... 'id name type size')
|
... 'id name type size')
|
||||||
# Names are separated by spaces or commas.
|
>>> # Names are separated by spaces or commas.
|
||||||
# 'id, name, type, size' would also work.
|
>>> # 'id, name, type, size' would also work.
|
||||||
>>> var_type._fields
|
>>> var_type._fields
|
||||||
('id', 'name', 'type', 'size')
|
('id', 'name', 'type', 'size')
|
||||||
|
|
||||||
|
@ -1929,7 +1930,9 @@ changes, or look through the Subversion logs for all the details.
|
||||||
|
|
||||||
* A new window method in the :mod:`curses` module,
|
* A new window method in the :mod:`curses` module,
|
||||||
:meth:`chgat`, changes the display attributes for a certain number of
|
:meth:`chgat`, changes the display attributes for a certain number of
|
||||||
characters on a single line. (Contributed by Fabian Kreutz.) ::
|
characters on a single line. (Contributed by Fabian Kreutz.)
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
# Boldface text starting at y=0,x=21
|
# Boldface text starting at y=0,x=21
|
||||||
# and affecting the rest of the line.
|
# and affecting the rest of the line.
|
||||||
|
@ -1999,7 +2002,7 @@ changes, or look through the Subversion logs for all the details.
|
||||||
order, and returns a new generator that returns the contents of all
|
order, and returns a new generator that returns the contents of all
|
||||||
the iterators, also in sorted order. For example::
|
the iterators, also in sorted order. For example::
|
||||||
|
|
||||||
heapq.merge([1, 3, 5, 9], [2, 8, 16]) ->
|
>>> list(heapq.merge([1, 3, 5, 9], [2, 8, 16]))
|
||||||
[1, 2, 3, 5, 8, 9, 16]
|
[1, 2, 3, 5, 8, 9, 16]
|
||||||
|
|
||||||
Another new function, ``heappushpop(heap, item)``,
|
Another new function, ``heappushpop(heap, item)``,
|
||||||
|
@ -2034,57 +2037,55 @@ changes, or look through the Subversion logs for all the details.
|
||||||
each of the elements; if some of the iterables are shorter than
|
each of the elements; if some of the iterables are shorter than
|
||||||
others, the missing values are set to *fillvalue*. For example::
|
others, the missing values are set to *fillvalue*. For example::
|
||||||
|
|
||||||
itertools.izip_longest([1,2,3], [1,2,3,4,5]) ->
|
>>> tuple(itertools.izip_longest([1,2,3], [1,2,3,4,5]))
|
||||||
(1, 1), (2, 2), (3, 3), (None, 4), (None, 5)
|
((1, 1), (2, 2), (3, 3), (None, 4), (None, 5))
|
||||||
|
|
||||||
``product(iter1, iter2, ..., [repeat=N])`` returns the Cartesian product
|
``product(iter1, iter2, ..., [repeat=N])`` returns the Cartesian product
|
||||||
of the supplied iterables, a set of tuples containing
|
of the supplied iterables, a set of tuples containing
|
||||||
every possible combination of the elements returned from each iterable. ::
|
every possible combination of the elements returned from each iterable. ::
|
||||||
|
|
||||||
itertools.product([1,2,3], [4,5,6]) ->
|
>>> list(itertools.product([1,2,3], [4,5,6]))
|
||||||
(1, 4), (1, 5), (1, 6),
|
[(1, 4), (1, 5), (1, 6),
|
||||||
(2, 4), (2, 5), (2, 6),
|
(2, 4), (2, 5), (2, 6),
|
||||||
(3, 4), (3, 5), (3, 6)
|
(3, 4), (3, 5), (3, 6)]
|
||||||
|
|
||||||
The optional *repeat* keyword argument is used for taking the
|
The optional *repeat* keyword argument is used for taking the
|
||||||
product of an iterable or a set of iterables with themselves,
|
product of an iterable or a set of iterables with themselves,
|
||||||
repeated *N* times. With a single iterable argument, *N*-tuples
|
repeated *N* times. With a single iterable argument, *N*-tuples
|
||||||
are returned::
|
are returned::
|
||||||
|
|
||||||
itertools.product([1,2], repeat=3) ->
|
>>> list(itertools.product([1,2], repeat=3))
|
||||||
(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2),
|
[(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2),
|
||||||
(2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)
|
(2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]
|
||||||
|
|
||||||
With two iterables, *2N*-tuples are returned. ::
|
With two iterables, *2N*-tuples are returned. ::
|
||||||
|
|
||||||
itertools.product([1,2], [3,4], repeat=2) ->
|
>>> list(itertools.product([1,2], [3,4], repeat=2))
|
||||||
(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4),
|
[(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4),
|
||||||
(1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4),
|
(1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4),
|
||||||
(2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4),
|
(2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4),
|
||||||
(2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 3), (2, 4, 2, 4)
|
(2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 3), (2, 4, 2, 4)]
|
||||||
|
|
||||||
``combinations(iterable, r)`` returns sub-sequences of length *r* from
|
``combinations(iterable, r)`` returns sub-sequences of length *r* from
|
||||||
the elements of *iterable*. ::
|
the elements of *iterable*. ::
|
||||||
|
|
||||||
itertools.combinations('123', 2) ->
|
>>> list(itertools.combinations('123', 2))
|
||||||
('1', '2'), ('1', '3'), ('2', '3')
|
[('1', '2'), ('1', '3'), ('2', '3')]
|
||||||
|
>>> list(itertools.combinations('123', 3))
|
||||||
itertools.combinations('123', 3) ->
|
[('1', '2', '3')]
|
||||||
('1', '2', '3')
|
>>> list(itertools.combinations('1234', 3))
|
||||||
|
[('1', '2', '3'), ('1', '2', '4'),
|
||||||
itertools.combinations('1234', 3) ->
|
('1', '3', '4'), ('2', '3', '4')]
|
||||||
('1', '2', '3'), ('1', '2', '4'), ('1', '3', '4'),
|
|
||||||
('2', '3', '4')
|
|
||||||
|
|
||||||
``permutations(iter[, r])`` returns all the permutations of length *r* of
|
``permutations(iter[, r])`` returns all the permutations of length *r* of
|
||||||
the iterable's elements. If *r* is not specified, it will default to the
|
the iterable's elements. If *r* is not specified, it will default to the
|
||||||
number of elements produced by the iterable. ::
|
number of elements produced by the iterable. ::
|
||||||
|
|
||||||
itertools.permutations([1,2,3,4], 2) ->
|
>>> list(itertools.permutations([1,2,3,4], 2))
|
||||||
(1, 2), (1, 3), (1, 4),
|
[(1, 2), (1, 3), (1, 4),
|
||||||
(2, 1), (2, 3), (2, 4),
|
(2, 1), (2, 3), (2, 4),
|
||||||
(3, 1), (3, 2), (3, 4),
|
(3, 1), (3, 2), (3, 4),
|
||||||
(4, 1), (4, 2), (4, 3)
|
(4, 1), (4, 2), (4, 3)]
|
||||||
|
|
||||||
``itertools.chain(*iterables)`` is an existing function in
|
``itertools.chain(*iterables)`` is an existing function in
|
||||||
:mod:`itertools` that gained a new constructor in Python 2.6.
|
:mod:`itertools` that gained a new constructor in Python 2.6.
|
||||||
|
@ -2093,8 +2094,8 @@ changes, or look through the Subversion logs for all the details.
|
||||||
then return all the elements of the first iterable, then
|
then return all the elements of the first iterable, then
|
||||||
all the elements of the second, and so on. ::
|
all the elements of the second, and so on. ::
|
||||||
|
|
||||||
chain.from_iterable([[1,2,3], [4,5,6]]) ->
|
>>> list(itertools.chain.from_iterable([[1,2,3], [4,5,6]]))
|
||||||
1, 2, 3, 4, 5, 6
|
[1, 2, 3, 4, 5, 6]
|
||||||
|
|
||||||
(All contributed by Raymond Hettinger.)
|
(All contributed by Raymond Hettinger.)
|
||||||
|
|
||||||
|
@ -2265,16 +2266,15 @@ changes, or look through the Subversion logs for all the details.
|
||||||
with an installed Python package. For example::
|
with an installed Python package. For example::
|
||||||
|
|
||||||
>>> import pkgutil
|
>>> import pkgutil
|
||||||
>>> pkgutil.get_data('test', 'exception_hierarchy.txt')
|
>>> print pkgutil.get_data('test', 'exception_hierarchy.txt')
|
||||||
'BaseException
|
BaseException
|
||||||
+-- SystemExit
|
+-- SystemExit
|
||||||
+-- KeyboardInterrupt
|
+-- KeyboardInterrupt
|
||||||
+-- GeneratorExit
|
+-- GeneratorExit
|
||||||
+-- Exception
|
+-- Exception
|
||||||
+-- StopIteration
|
+-- StopIteration
|
||||||
+-- StandardError
|
+-- StandardError
|
||||||
...'
|
...
|
||||||
>>>
|
|
||||||
|
|
||||||
(Contributed by Paul Moore; :issue:`2439`.)
|
(Contributed by Paul Moore; :issue:`2439`.)
|
||||||
|
|
||||||
|
@ -2548,7 +2548,7 @@ changes, or look through the Subversion logs for all the details.
|
||||||
|
|
||||||
with test_support.check_warnings() as wrec:
|
with test_support.check_warnings() as wrec:
|
||||||
warnings.simplefilter("always")
|
warnings.simplefilter("always")
|
||||||
... code that triggers a warning ...
|
# ... code that triggers a warning ...
|
||||||
assert str(wrec.message) == "function is outdated"
|
assert str(wrec.message) == "function is outdated"
|
||||||
assert len(wrec.warnings) == 1, "Multiple warnings raised"
|
assert len(wrec.warnings) == 1, "Multiple warnings raised"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue