fixed foot-notes; added %b and clarified %s as a synonym for %b
This commit is contained in:
parent
2350056bf5
commit
0eb27066c4
33
pep-0461.txt
33
pep-0461.txt
|
@ -94,14 +94,22 @@ Examples::
|
||||||
``format`` numerics (binary), and 2) it will make 2/3 code easier as Python 2.x
|
``format`` numerics (binary), and 2) it will make 2/3 code easier as Python 2.x
|
||||||
code uses ``%s``; however, it is restricted in what it will accept:
|
code uses ``%s``; however, it is restricted in what it will accept:
|
||||||
|
|
||||||
- input type supports ``Py_buffer`` [6]_?
|
- input type supports ``Py_buffer`` [4]_?
|
||||||
use it to collect the necessary bytes
|
use it to collect the necessary bytes
|
||||||
|
|
||||||
- input type is something else?
|
- input type is something else?
|
||||||
use its ``__bytes__`` method [7]_ ; if there isn't one, raise a ``TypeError``
|
use its ``__bytes__`` method [5]_ ; if there isn't one, raise a ``TypeError``
|
||||||
|
|
||||||
In particular, ``%s`` will not accept numbers (use a numeric format code for
|
In particular, ``%s`` will not accept numbers nor ``str``. ``str`` is rejected
|
||||||
that), nor ``str`` (encode it to ``bytes``).
|
as the string to bytes conversion requires an encoding, and we are refusing to
|
||||||
|
guess; numbers are rejected because:
|
||||||
|
|
||||||
|
- what makes a number is fuzzy (float? Decimal? Fraction? some user type?)
|
||||||
|
|
||||||
|
- allowing numbers would lead to ambiguity between numbers and textual
|
||||||
|
representations of numbers (3.14 vs '3.14')
|
||||||
|
|
||||||
|
- given the nature of wire formats, explicit is definitely better than implicit
|
||||||
|
|
||||||
Examples::
|
Examples::
|
||||||
|
|
||||||
|
@ -128,7 +136,7 @@ value. Use cases include developing a new protocol and writing landmarks
|
||||||
into the stream; debugging data going into an existing protocol to see if
|
into the stream; debugging data going into an existing protocol to see if
|
||||||
the problem is the protocol itself or bad data; a fall-back for a serialization
|
the problem is the protocol itself or bad data; a fall-back for a serialization
|
||||||
format; or any situation where defining ``__bytes__`` would not be appropriate
|
format; or any situation where defining ``__bytes__`` would not be appropriate
|
||||||
but a readable/informative representation is needed [8]_.
|
but a readable/informative representation is needed [6]_.
|
||||||
|
|
||||||
Examples::
|
Examples::
|
||||||
|
|
||||||
|
@ -178,17 +186,18 @@ Various new special methods were proposed, such as ``__ascii__``,
|
||||||
``__format_bytes__``, etc.; such methods are not needed at this time, but can
|
``__format_bytes__``, etc.; such methods are not needed at this time, but can
|
||||||
be visited again later if real-world use shows deficiencies with this solution.
|
be visited again later if real-world use shows deficiencies with this solution.
|
||||||
|
|
||||||
A competing PEP, ``PEP 460 Add binary interpolation and formatting`` [9]_,
|
A competing PEP, ``PEP 460 Add binary interpolation and formatting`` [7]_,
|
||||||
also exists.
|
also exists.
|
||||||
|
|
||||||
|
|
||||||
Objections
|
Objections
|
||||||
==========
|
==========
|
||||||
|
|
||||||
The objections raised against this PEP were mainly variations on two themes::
|
The objections raised against this PEP were mainly variations on two themes:
|
||||||
|
|
||||||
- the ``bytes`` and ``bytearray`` types are for pure binary data, with no
|
- the ``bytes`` and ``bytearray`` types are for pure binary data, with no
|
||||||
assumptions about encodings
|
assumptions about encodings
|
||||||
|
|
||||||
- offering %-interpolation that assumes an ASCII encoding will be an
|
- offering %-interpolation that assumes an ASCII encoding will be an
|
||||||
attractive nuisance and lead us back to the problems of the Python 2
|
attractive nuisance and lead us back to the problems of the Python 2
|
||||||
``str``/``unicode`` text model
|
``str``/``unicode`` text model
|
||||||
|
@ -213,13 +222,11 @@ Footnotes
|
||||||
.. [1] http://docs.python.org/2/library/stdtypes.html#string-formatting
|
.. [1] http://docs.python.org/2/library/stdtypes.html#string-formatting
|
||||||
.. [2] neither string.Template, format, nor str.format are under consideration
|
.. [2] neither string.Template, format, nor str.format are under consideration
|
||||||
.. [3] https://mail.python.org/pipermail/python-dev/2014-January/131518.html
|
.. [3] https://mail.python.org/pipermail/python-dev/2014-January/131518.html
|
||||||
.. [4] to use a str object in a bytes interpolation, encode it first
|
.. [4] http://docs.python.org/3/c-api/buffer.html
|
||||||
.. [5] %c is not an exception as neither of its possible arguments are str
|
|
||||||
.. [6] http://docs.python.org/3/c-api/buffer.html
|
|
||||||
examples: ``memoryview``, ``array.array``, ``bytearray``, ``bytes``
|
examples: ``memoryview``, ``array.array``, ``bytearray``, ``bytes``
|
||||||
.. [7] http://docs.python.org/3/reference/datamodel.html#object.__bytes__
|
.. [5] http://docs.python.org/3/reference/datamodel.html#object.__bytes__
|
||||||
.. [8] https://mail.python.org/pipermail/python-dev/2014-February/132750.html
|
.. [6] https://mail.python.org/pipermail/python-dev/2014-February/132750.html
|
||||||
.. [9] http://python.org/dev/peps/pep-0460/
|
.. [7] http://python.org/dev/peps/pep-0460/
|
||||||
|
|
||||||
|
|
||||||
Copyright
|
Copyright
|
||||||
|
|
Loading…
Reference in New Issue