mirror of https://github.com/python/peps
363 lines
30 KiB
HTML
363 lines
30 KiB
HTML
|
||
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<meta name="color-scheme" content="light dark">
|
||
<title>PEP 461 – Adding % formatting to bytes and bytearray | peps.python.org</title>
|
||
<link rel="shortcut icon" href="../_static/py.png">
|
||
<link rel="canonical" href="https://peps.python.org/pep-0461/">
|
||
<link rel="stylesheet" href="../_static/style.css" type="text/css">
|
||
<link rel="stylesheet" href="../_static/mq.css" type="text/css">
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" media="(prefers-color-scheme: light)" id="pyg-light">
|
||
<link rel="stylesheet" href="../_static/pygments_dark.css" type="text/css" media="(prefers-color-scheme: dark)" id="pyg-dark">
|
||
<link rel="alternate" type="application/rss+xml" title="Latest PEPs" href="https://peps.python.org/peps.rss">
|
||
<meta property="og:title" content='PEP 461 – Adding % formatting to bytes and bytearray | peps.python.org'>
|
||
<meta property="og:type" content="website">
|
||
<meta property="og:url" content="https://peps.python.org/pep-0461/">
|
||
<meta property="og:site_name" content="Python Enhancement Proposals (PEPs)">
|
||
<meta property="og:image" content="https://peps.python.org/_static/og-image.png">
|
||
<meta property="og:image:alt" content="Python PEPs">
|
||
<meta property="og:image:width" content="200">
|
||
<meta property="og:image:height" content="200">
|
||
<meta name="description" content="Python Enhancement Proposals (PEPs)">
|
||
<meta name="theme-color" content="#3776ab">
|
||
</head>
|
||
<body>
|
||
|
||
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
|
||
<symbol id="svg-sun-half" viewBox="0 0 24 24" pointer-events="all">
|
||
<title>Following system colour scheme</title>
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none"
|
||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||
<circle cx="12" cy="12" r="9"></circle>
|
||
<path d="M12 3v18m0-12l4.65-4.65M12 14.3l7.37-7.37M12 19.6l8.85-8.85"></path>
|
||
</svg>
|
||
</symbol>
|
||
<symbol id="svg-moon" viewBox="0 0 24 24" pointer-events="all">
|
||
<title>Selected dark colour scheme</title>
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none"
|
||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z"></path>
|
||
</svg>
|
||
</symbol>
|
||
<symbol id="svg-sun" viewBox="0 0 24 24" pointer-events="all">
|
||
<title>Selected light colour scheme</title>
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none"
|
||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||
<circle cx="12" cy="12" r="5"></circle>
|
||
<line x1="12" y1="1" x2="12" y2="3"></line>
|
||
<line x1="12" y1="21" x2="12" y2="23"></line>
|
||
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
|
||
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
|
||
<line x1="1" y1="12" x2="3" y2="12"></line>
|
||
<line x1="21" y1="12" x2="23" y2="12"></line>
|
||
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
|
||
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
|
||
</svg>
|
||
</symbol>
|
||
</svg>
|
||
<script>
|
||
|
||
document.documentElement.dataset.colour_scheme = localStorage.getItem("colour_scheme") || "auto"
|
||
</script>
|
||
<section id="pep-page-section">
|
||
<header>
|
||
<h1>Python Enhancement Proposals</h1>
|
||
<ul class="breadcrumbs">
|
||
<li><a href="https://www.python.org/" title="The Python Programming Language">Python</a> » </li>
|
||
<li><a href="../pep-0000/">PEP Index</a> » </li>
|
||
<li>PEP 461</li>
|
||
</ul>
|
||
<button id="colour-scheme-cycler" onClick="setColourScheme(nextColourScheme())">
|
||
<svg aria-hidden="true" class="colour-scheme-icon-when-auto"><use href="#svg-sun-half"></use></svg>
|
||
<svg aria-hidden="true" class="colour-scheme-icon-when-dark"><use href="#svg-moon"></use></svg>
|
||
<svg aria-hidden="true" class="colour-scheme-icon-when-light"><use href="#svg-sun"></use></svg>
|
||
<span class="visually-hidden">Toggle light / dark / auto colour theme</span>
|
||
</button>
|
||
</header>
|
||
<article>
|
||
<section id="pep-content">
|
||
<h1 class="page-title">PEP 461 – Adding % formatting to bytes and bytearray</h1>
|
||
<dl class="rfc2822 field-list simple">
|
||
<dt class="field-odd">Author<span class="colon">:</span></dt>
|
||
<dd class="field-odd">Ethan Furman <ethan at stoneleaf.us></dd>
|
||
<dt class="field-even">Status<span class="colon">:</span></dt>
|
||
<dd class="field-even"><abbr title="Accepted and implementation complete, or no longer active">Final</abbr></dd>
|
||
<dt class="field-odd">Type<span class="colon">:</span></dt>
|
||
<dd class="field-odd"><abbr title="Normative PEP with a new feature for Python, implementation change for CPython or interoperability standard for the ecosystem">Standards Track</abbr></dd>
|
||
<dt class="field-even">Created<span class="colon">:</span></dt>
|
||
<dd class="field-even">13-Jan-2014</dd>
|
||
<dt class="field-odd">Python-Version<span class="colon">:</span></dt>
|
||
<dd class="field-odd">3.5</dd>
|
||
<dt class="field-even">Post-History<span class="colon">:</span></dt>
|
||
<dd class="field-even">14-Jan-2014, 15-Jan-2014, 17-Jan-2014, 22-Feb-2014, 25-Mar-2014,
|
||
27-Mar-2014</dd>
|
||
<dt class="field-odd">Resolution<span class="colon">:</span></dt>
|
||
<dd class="field-odd"><a class="reference external" href="https://mail.python.org/pipermail/python-dev/2014-March/133621.html">Python-Dev message</a></dd>
|
||
</dl>
|
||
<hr class="docutils" />
|
||
<section id="contents">
|
||
<details><summary>Table of Contents</summary><ul class="simple">
|
||
<li><a class="reference internal" href="#abstract">Abstract</a></li>
|
||
<li><a class="reference internal" href="#rationale">Rationale</a></li>
|
||
<li><a class="reference internal" href="#motivation">Motivation</a></li>
|
||
<li><a class="reference internal" href="#proposed-semantics-for-bytes-and-bytearray-formatting">Proposed semantics for <code class="docutils literal notranslate"><span class="pre">bytes</span></code> and <code class="docutils literal notranslate"><span class="pre">bytearray</span></code> formatting</a><ul>
|
||
<li><a class="reference internal" href="#interpolation">%-interpolation</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#compatibility-with-python-2">Compatibility with Python 2</a></li>
|
||
<li><a class="reference internal" href="#proposed-variations">Proposed variations</a></li>
|
||
<li><a class="reference internal" href="#objections">Objections</a></li>
|
||
<li><a class="reference internal" href="#footnotes">Footnotes</a></li>
|
||
<li><a class="reference internal" href="#copyright">Copyright</a></li>
|
||
</ul>
|
||
</details></section>
|
||
<section id="abstract">
|
||
<h2><a class="toc-backref" href="#abstract" role="doc-backlink">Abstract</a></h2>
|
||
<p>This PEP proposes adding % formatting operations similar to Python 2’s <code class="docutils literal notranslate"><span class="pre">str</span></code>
|
||
type to <code class="docutils literal notranslate"><span class="pre">bytes</span></code> and <code class="docutils literal notranslate"><span class="pre">bytearray</span></code> <a class="footnote-reference brackets" href="#id9" id="id1">[1]</a> <a class="footnote-reference brackets" href="#id10" id="id2">[2]</a>.</p>
|
||
</section>
|
||
<section id="rationale">
|
||
<h2><a class="toc-backref" href="#rationale" role="doc-backlink">Rationale</a></h2>
|
||
<p>While interpolation is usually thought of as a string operation, there are
|
||
cases where interpolation on <code class="docutils literal notranslate"><span class="pre">bytes</span></code> or <code class="docutils literal notranslate"><span class="pre">bytearrays</span></code> make sense, and the
|
||
work needed to make up for this missing functionality detracts from the overall
|
||
readability of the code.</p>
|
||
</section>
|
||
<section id="motivation">
|
||
<h2><a class="toc-backref" href="#motivation" role="doc-backlink">Motivation</a></h2>
|
||
<p>With Python 3 and the split between <code class="docutils literal notranslate"><span class="pre">str</span></code> and <code class="docutils literal notranslate"><span class="pre">bytes</span></code>, one small but
|
||
important area of programming became slightly more difficult, and much more
|
||
painful – wire format protocols <a class="footnote-reference brackets" href="#id11" id="id3">[3]</a>.</p>
|
||
<p>This area of programming is characterized by a mixture of binary data and
|
||
ASCII compatible segments of text (aka ASCII-encoded text). Bringing back a
|
||
restricted %-interpolation for <code class="docutils literal notranslate"><span class="pre">bytes</span></code> and <code class="docutils literal notranslate"><span class="pre">bytearray</span></code> will aid both in
|
||
writing new wire format code, and in porting Python 2 wire format code.</p>
|
||
<p>Common use-cases include <code class="docutils literal notranslate"><span class="pre">dbf</span></code> and <code class="docutils literal notranslate"><span class="pre">pdf</span></code> file formats, <code class="docutils literal notranslate"><span class="pre">email</span></code>
|
||
formats, and <code class="docutils literal notranslate"><span class="pre">FTP</span></code> and <code class="docutils literal notranslate"><span class="pre">HTTP</span></code> communications, among many others.</p>
|
||
</section>
|
||
<section id="proposed-semantics-for-bytes-and-bytearray-formatting">
|
||
<h2><a class="toc-backref" href="#proposed-semantics-for-bytes-and-bytearray-formatting" role="doc-backlink">Proposed semantics for <code class="docutils literal notranslate"><span class="pre">bytes</span></code> and <code class="docutils literal notranslate"><span class="pre">bytearray</span></code> formatting</a></h2>
|
||
<section id="interpolation">
|
||
<h3><a class="toc-backref" href="#interpolation" role="doc-backlink">%-interpolation</a></h3>
|
||
<p>All the numeric formatting codes (<code class="docutils literal notranslate"><span class="pre">d</span></code>, <code class="docutils literal notranslate"><span class="pre">i</span></code>, <code class="docutils literal notranslate"><span class="pre">o</span></code>, <code class="docutils literal notranslate"><span class="pre">u</span></code>, <code class="docutils literal notranslate"><span class="pre">x</span></code>, <code class="docutils literal notranslate"><span class="pre">X</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">e</span></code>, <code class="docutils literal notranslate"><span class="pre">E</span></code>, <code class="docutils literal notranslate"><span class="pre">f</span></code>, <code class="docutils literal notranslate"><span class="pre">F</span></code>, <code class="docutils literal notranslate"><span class="pre">g</span></code>, <code class="docutils literal notranslate"><span class="pre">G</span></code>, and any that are subsequently added
|
||
to Python 3) will be supported, and will work as they do for str, including
|
||
the padding, justification and other related modifiers (currently <code class="docutils literal notranslate"><span class="pre">#</span></code>, <code class="docutils literal notranslate"><span class="pre">0</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">-</span></code>, space, and <code class="docutils literal notranslate"><span class="pre">+</span></code> (plus any added to Python 3)). The only
|
||
non-numeric codes allowed are <code class="docutils literal notranslate"><span class="pre">c</span></code>, <code class="docutils literal notranslate"><span class="pre">b</span></code>, <code class="docutils literal notranslate"><span class="pre">a</span></code>, and <code class="docutils literal notranslate"><span class="pre">s</span></code> (which is a
|
||
synonym for b).</p>
|
||
<p>For the numeric codes, the only difference between <code class="docutils literal notranslate"><span class="pre">str</span></code> and <code class="docutils literal notranslate"><span class="pre">bytes</span></code> (or
|
||
<code class="docutils literal notranslate"><span class="pre">bytearray</span></code>) interpolation is that the results from these codes will be
|
||
ASCII-encoded text, not unicode. In other words, for any numeric formatting
|
||
code <code class="docutils literal notranslate"><span class="pre">%x</span></code>:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="sa">b</span><span class="s2">"</span><span class="si">%x</span><span class="s2">"</span> <span class="o">%</span> <span class="n">val</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>is equivalent to:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="s2">"</span><span class="si">%x</span><span class="s2">"</span> <span class="o">%</span> <span class="n">val</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s2">"ascii"</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Examples:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="sa">b</span><span class="s1">'</span><span class="si">%4x</span><span class="s1">'</span> <span class="o">%</span> <span class="mi">10</span>
|
||
<span class="go">b' a'</span>
|
||
|
||
<span class="gp">>>> </span><span class="sa">b</span><span class="s1">'</span><span class="si">%#4x</span><span class="s1">'</span> <span class="o">%</span> <span class="mi">10</span>
|
||
<span class="go">' 0xa'</span>
|
||
|
||
<span class="gp">>>> </span><span class="sa">b</span><span class="s1">'</span><span class="si">%04X</span><span class="s1">'</span> <span class="o">%</span> <span class="mi">10</span>
|
||
<span class="go">'000A'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p><code class="docutils literal notranslate"><span class="pre">%c</span></code> will insert a single byte, either from an <code class="docutils literal notranslate"><span class="pre">int</span></code> in range(256), or from
|
||
a <code class="docutils literal notranslate"><span class="pre">bytes</span></code> argument of length 1, not from a <code class="docutils literal notranslate"><span class="pre">str</span></code>.</p>
|
||
<p>Examples:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="sa">b</span><span class="s1">'</span><span class="si">%c</span><span class="s1">'</span> <span class="o">%</span> <span class="mi">48</span>
|
||
<span class="go">b'0'</span>
|
||
|
||
<span class="gp">>>> </span><span class="sa">b</span><span class="s1">'</span><span class="si">%c</span><span class="s1">'</span> <span class="o">%</span> <span class="sa">b</span><span class="s1">'a'</span>
|
||
<span class="go">b'a'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p><code class="docutils literal notranslate"><span class="pre">%b</span></code> will insert a series of bytes. These bytes are collected in one of two
|
||
ways:</p>
|
||
<ul class="simple">
|
||
<li>input type supports <code class="docutils literal notranslate"><span class="pre">Py_buffer</span></code> <a class="footnote-reference brackets" href="#id12" id="id4">[4]</a>?
|
||
use it to collect the necessary bytes</li>
|
||
<li>input type is something else?
|
||
use its <code class="docutils literal notranslate"><span class="pre">__bytes__</span></code> method <a class="footnote-reference brackets" href="#id13" id="id5">[5]</a> ; if there isn’t one, raise a <code class="docutils literal notranslate"><span class="pre">TypeError</span></code></li>
|
||
</ul>
|
||
<p>In particular, <code class="docutils literal notranslate"><span class="pre">%b</span></code> will not accept numbers nor <code class="docutils literal notranslate"><span class="pre">str</span></code>. <code class="docutils literal notranslate"><span class="pre">str</span></code> is rejected
|
||
as the string to bytes conversion requires an encoding, and we are refusing to
|
||
guess; numbers are rejected because:</p>
|
||
<ul class="simple">
|
||
<li>what makes a number is fuzzy (float? Decimal? Fraction? some user type?)</li>
|
||
<li>allowing numbers would lead to ambiguity between numbers and textual
|
||
representations of numbers (3.14 vs ‘3.14’)</li>
|
||
<li>given the nature of wire formats, explicit is definitely better than implicit</li>
|
||
</ul>
|
||
<p><code class="docutils literal notranslate"><span class="pre">%s</span></code> is included as a synonym for <code class="docutils literal notranslate"><span class="pre">%b</span></code> for the sole purpose of making 2/3 code
|
||
bases easier to maintain. Python 3 only code should use <code class="docutils literal notranslate"><span class="pre">%b</span></code>.</p>
|
||
<p>Examples:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="sa">b</span><span class="s1">'%b'</span> <span class="o">%</span> <span class="sa">b</span><span class="s1">'abc'</span>
|
||
<span class="go">b'abc'</span>
|
||
|
||
<span class="gp">>>> </span><span class="sa">b</span><span class="s1">'%b'</span> <span class="o">%</span> <span class="s1">'some string'</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">'utf8'</span><span class="p">)</span>
|
||
<span class="go">b'some string'</span>
|
||
|
||
<span class="gp">>>> </span><span class="sa">b</span><span class="s1">'%b'</span> <span class="o">%</span> <span class="mf">3.14</span>
|
||
<span class="gt">Traceback (most recent call last):</span>
|
||
<span class="c">...</span>
|
||
<span class="gr">TypeError</span>: <span class="n">b'%b' does not accept 'float'</span>
|
||
|
||
<span class="gp">>>> </span><span class="sa">b</span><span class="s1">'%b'</span> <span class="o">%</span> <span class="s1">'hello world!'</span>
|
||
<span class="gt">Traceback (most recent call last):</span>
|
||
<span class="c">...</span>
|
||
<span class="gr">TypeError</span>: <span class="n">b'%b' does not accept 'str'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p><code class="docutils literal notranslate"><span class="pre">%a</span></code> will give the equivalent of
|
||
<code class="docutils literal notranslate"><span class="pre">repr(some_obj).encode('ascii',</span> <span class="pre">'backslashreplace')</span></code> on the interpolated
|
||
value. Use cases include developing a new protocol and writing landmarks
|
||
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
|
||
format; or any situation where defining <code class="docutils literal notranslate"><span class="pre">__bytes__</span></code> would not be appropriate
|
||
but a readable/informative representation is needed <a class="footnote-reference brackets" href="#id14" id="id6">[6]</a>.</p>
|
||
<p><code class="docutils literal notranslate"><span class="pre">%r</span></code> is included as a synonym for <code class="docutils literal notranslate"><span class="pre">%a</span></code> for the sole purpose of making 2/3
|
||
code bases easier to maintain. Python 3 only code use <code class="docutils literal notranslate"><span class="pre">%a</span></code> <a class="footnote-reference brackets" href="#id15" id="id7">[7]</a>.</p>
|
||
<p>Examples:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="sa">b</span><span class="s1">'</span><span class="si">%a</span><span class="s1">'</span> <span class="o">%</span> <span class="mf">3.14</span>
|
||
<span class="go">b'3.14'</span>
|
||
|
||
<span class="gp">>>> </span><span class="sa">b</span><span class="s1">'</span><span class="si">%a</span><span class="s1">'</span> <span class="o">%</span> <span class="sa">b</span><span class="s1">'abc'</span>
|
||
<span class="go">b"b'abc'"</span>
|
||
|
||
<span class="gp">>>> </span><span class="sa">b</span><span class="s1">'</span><span class="si">%a</span><span class="s1">'</span> <span class="o">%</span> <span class="s1">'def'</span>
|
||
<span class="go">b"'def'"</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="compatibility-with-python-2">
|
||
<h2><a class="toc-backref" href="#compatibility-with-python-2" role="doc-backlink">Compatibility with Python 2</a></h2>
|
||
<p>As noted above, <code class="docutils literal notranslate"><span class="pre">%s</span></code> and <code class="docutils literal notranslate"><span class="pre">%r</span></code> are being included solely to help ease
|
||
migration from, and/or have a single code base with, Python 2. This is
|
||
important as there are modules both in the wild and behind closed doors that
|
||
currently use the Python 2 <code class="docutils literal notranslate"><span class="pre">str</span></code> type as a <code class="docutils literal notranslate"><span class="pre">bytes</span></code> container, and hence
|
||
are using <code class="docutils literal notranslate"><span class="pre">%s</span></code> as a bytes interpolator.</p>
|
||
<p>However, <code class="docutils literal notranslate"><span class="pre">%b</span></code> and <code class="docutils literal notranslate"><span class="pre">%a</span></code> should be used in new, Python 3 only code, so <code class="docutils literal notranslate"><span class="pre">%s</span></code>
|
||
and <code class="docutils literal notranslate"><span class="pre">%r</span></code> will immediately be deprecated, but not removed from the 3.x series
|
||
<a class="footnote-reference brackets" href="#id15" id="id8">[7]</a>.</p>
|
||
</section>
|
||
<section id="proposed-variations">
|
||
<h2><a class="toc-backref" href="#proposed-variations" role="doc-backlink">Proposed variations</a></h2>
|
||
<p>It has been proposed to automatically use <code class="docutils literal notranslate"><span class="pre">.encode('ascii','strict')</span></code> for
|
||
<code class="docutils literal notranslate"><span class="pre">str</span></code> arguments to <code class="docutils literal notranslate"><span class="pre">%b</span></code>.</p>
|
||
<ul class="simple">
|
||
<li>Rejected as this would lead to intermittent failures. Better to have the
|
||
operation always fail so the trouble-spot can be correctly fixed.</li>
|
||
</ul>
|
||
<p>It has been proposed to have <code class="docutils literal notranslate"><span class="pre">%b</span></code> return the ascii-encoded repr when the
|
||
value is a <code class="docutils literal notranslate"><span class="pre">str</span></code> (b’%b’ % ‘abc’ –> b“‘abc’”).</p>
|
||
<ul class="simple">
|
||
<li>Rejected as this would lead to hard to debug failures far from the problem
|
||
site. Better to have the operation always fail so the trouble-spot can be
|
||
easily fixed.</li>
|
||
</ul>
|
||
<p>Originally this PEP also proposed adding format-style formatting, but it was
|
||
decided that format and its related machinery were all strictly text (aka
|
||
<code class="docutils literal notranslate"><span class="pre">str</span></code>) based, and it was dropped.</p>
|
||
<p>Various new special methods were proposed, such as <code class="docutils literal notranslate"><span class="pre">__ascii__</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">__format_bytes__</span></code>, etc.; such methods are not needed at this time, but can
|
||
be visited again later if real-world use shows deficiencies with this solution.</p>
|
||
<p>A competing PEP, <a class="pep reference internal" href="../pep-0460/" title="PEP 460 – Add binary interpolation and formatting">PEP 460 Add binary interpolation and formatting</a>,
|
||
also exists.</p>
|
||
</section>
|
||
<section id="objections">
|
||
<h2><a class="toc-backref" href="#objections" role="doc-backlink">Objections</a></h2>
|
||
<p>The objections raised against this PEP were mainly variations on two themes:</p>
|
||
<ul class="simple">
|
||
<li>the <code class="docutils literal notranslate"><span class="pre">bytes</span></code> and <code class="docutils literal notranslate"><span class="pre">bytearray</span></code> types are for pure binary data, with no
|
||
assumptions about encodings</li>
|
||
<li>offering %-interpolation that assumes an ASCII encoding will be an
|
||
attractive nuisance and lead us back to the problems of the Python 2
|
||
<code class="docutils literal notranslate"><span class="pre">str</span></code>/<code class="docutils literal notranslate"><span class="pre">unicode</span></code> text model</li>
|
||
</ul>
|
||
<p>As was seen during the discussion, <code class="docutils literal notranslate"><span class="pre">bytes</span></code> and <code class="docutils literal notranslate"><span class="pre">bytearray</span></code> are also used
|
||
for mixed binary data and ASCII-compatible segments: file formats such as
|
||
<code class="docutils literal notranslate"><span class="pre">dbf</span></code> and <code class="docutils literal notranslate"><span class="pre">pdf</span></code>, network protocols such as <code class="docutils literal notranslate"><span class="pre">ftp</span></code> and <code class="docutils literal notranslate"><span class="pre">email</span></code>, etc.</p>
|
||
<p><code class="docutils literal notranslate"><span class="pre">bytes</span></code> and <code class="docutils literal notranslate"><span class="pre">bytearray</span></code> already have several methods which assume an ASCII
|
||
compatible encoding. <code class="docutils literal notranslate"><span class="pre">upper()</span></code>, <code class="docutils literal notranslate"><span class="pre">isalpha()</span></code>, and <code class="docutils literal notranslate"><span class="pre">expandtabs()</span></code> to name
|
||
just a few. %-interpolation, with its very restricted mini-language, will not
|
||
be any more of a nuisance than the already existing methods.</p>
|
||
<p>Some have objected to allowing the full range of numeric formatting codes with
|
||
the claim that decimal alone would be sufficient. However, at least two
|
||
formats (dbf and pdf) make use of non-decimal numbers.</p>
|
||
</section>
|
||
<section id="footnotes">
|
||
<h2><a class="toc-backref" href="#footnotes" role="doc-backlink">Footnotes</a></h2>
|
||
<aside class="footnote-list brackets">
|
||
<aside class="footnote brackets" id="id9" role="doc-footnote">
|
||
<dt class="label" id="id9">[<a href="#id1">1</a>]</dt>
|
||
<dd><a class="reference external" href="http://docs.python.org/2/library/stdtypes.html#string-formatting">http://docs.python.org/2/library/stdtypes.html#string-formatting</a></aside>
|
||
<aside class="footnote brackets" id="id10" role="doc-footnote">
|
||
<dt class="label" id="id10">[<a href="#id2">2</a>]</dt>
|
||
<dd>neither string.Template, format, nor str.format are under consideration</aside>
|
||
<aside class="footnote brackets" id="id11" role="doc-footnote">
|
||
<dt class="label" id="id11">[<a href="#id3">3</a>]</dt>
|
||
<dd><a class="reference external" href="https://mail.python.org/pipermail/python-dev/2014-January/131518.html">https://mail.python.org/pipermail/python-dev/2014-January/131518.html</a></aside>
|
||
<aside class="footnote brackets" id="id12" role="doc-footnote">
|
||
<dt class="label" id="id12">[<a href="#id4">4</a>]</dt>
|
||
<dd><a class="reference external" href="http://docs.python.org/3/c-api/buffer.html">http://docs.python.org/3/c-api/buffer.html</a>
|
||
examples: <code class="docutils literal notranslate"><span class="pre">memoryview</span></code>, <code class="docutils literal notranslate"><span class="pre">array.array</span></code>, <code class="docutils literal notranslate"><span class="pre">bytearray</span></code>, <code class="docutils literal notranslate"><span class="pre">bytes</span></code></aside>
|
||
<aside class="footnote brackets" id="id13" role="doc-footnote">
|
||
<dt class="label" id="id13">[<a href="#id5">5</a>]</dt>
|
||
<dd><a class="reference external" href="http://docs.python.org/3/reference/datamodel.html#object.__bytes__">http://docs.python.org/3/reference/datamodel.html#object.__bytes__</a></aside>
|
||
<aside class="footnote brackets" id="id14" role="doc-footnote">
|
||
<dt class="label" id="id14">[<a href="#id6">6</a>]</dt>
|
||
<dd><a class="reference external" href="https://mail.python.org/pipermail/python-dev/2014-February/132750.html">https://mail.python.org/pipermail/python-dev/2014-February/132750.html</a></aside>
|
||
<aside class="footnote brackets" id="id15" role="doc-footnote">
|
||
<dt class="label" id="id15">[7]<em> (<a href='#id7'>1</a>, <a href='#id8'>2</a>) </em></dt>
|
||
<dd><a class="reference external" href="http://bugs.python.org/issue23467">http://bugs.python.org/issue23467</a> – originally <code class="docutils literal notranslate"><span class="pre">%r</span></code> was not allowed,
|
||
but was added for consistency during the 3.5 alpha stage.</aside>
|
||
</aside>
|
||
</section>
|
||
<section id="copyright">
|
||
<h2><a class="toc-backref" href="#copyright" role="doc-backlink">Copyright</a></h2>
|
||
<p>This document has been placed in the public domain.</p>
|
||
</section>
|
||
</section>
|
||
<hr class="docutils" />
|
||
<p>Source: <a class="reference external" href="https://github.com/python/peps/blob/main/peps/pep-0461.rst">https://github.com/python/peps/blob/main/peps/pep-0461.rst</a></p>
|
||
<p>Last modified: <a class="reference external" href="https://github.com/python/peps/commits/main/peps/pep-0461.rst">2023-09-09 17:39:29 GMT</a></p>
|
||
|
||
</article>
|
||
<nav id="pep-sidebar">
|
||
<h2>Contents</h2>
|
||
<ul>
|
||
<li><a class="reference internal" href="#abstract">Abstract</a></li>
|
||
<li><a class="reference internal" href="#rationale">Rationale</a></li>
|
||
<li><a class="reference internal" href="#motivation">Motivation</a></li>
|
||
<li><a class="reference internal" href="#proposed-semantics-for-bytes-and-bytearray-formatting">Proposed semantics for <code class="docutils literal notranslate"><span class="pre">bytes</span></code> and <code class="docutils literal notranslate"><span class="pre">bytearray</span></code> formatting</a><ul>
|
||
<li><a class="reference internal" href="#interpolation">%-interpolation</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#compatibility-with-python-2">Compatibility with Python 2</a></li>
|
||
<li><a class="reference internal" href="#proposed-variations">Proposed variations</a></li>
|
||
<li><a class="reference internal" href="#objections">Objections</a></li>
|
||
<li><a class="reference internal" href="#footnotes">Footnotes</a></li>
|
||
<li><a class="reference internal" href="#copyright">Copyright</a></li>
|
||
</ul>
|
||
|
||
<br>
|
||
<a id="source" href="https://github.com/python/peps/blob/main/peps/pep-0461.rst">Page Source (GitHub)</a>
|
||
</nav>
|
||
</section>
|
||
<script src="../_static/colour_scheme.js"></script>
|
||
<script src="../_static/wrap_tables.js"></script>
|
||
<script src="../_static/sticky_banner.js"></script>
|
||
</body>
|
||
</html> |