python-peps/pep-3111/index.html

268 lines
17 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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 3111 Simple input built-in in Python 3000 | peps.python.org</title>
<link rel="shortcut icon" href="../_static/py.png">
<link rel="canonical" href="https://peps.python.org/pep-3111/">
<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 3111 Simple input built-in in Python 3000 | peps.python.org'>
<meta property="og:description" content="Input and output are core features of computer programs. Currently, Python provides a simple means of output through the print keyword and two simple means of interactive input through the input() and raw_input() built-in functions.">
<meta property="og:type" content="website">
<meta property="og:url" content="https://peps.python.org/pep-3111/">
<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="Input and output are core features of computer programs. Currently, Python provides a simple means of output through the print keyword and two simple means of interactive input through the input() and raw_input() built-in functions.">
<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> &raquo; </li>
<li><a href="../pep-0000/">PEP Index</a> &raquo; </li>
<li>PEP 3111</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 3111 Simple input built-in in Python 3000</h1>
<dl class="rfc2822 field-list simple">
<dt class="field-odd">Author<span class="colon">:</span></dt>
<dd class="field-odd">Andre Roberge &lt;andre.roberge at gmail.com&gt;</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-Sep-2006</dd>
<dt class="field-odd">Python-Version<span class="colon">:</span></dt>
<dd class="field-odd">3.0</dd>
<dt class="field-even">Post-History<span class="colon">:</span></dt>
<dd class="field-even">22-Dec-2006</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="#motivation">Motivation</a></li>
<li><a class="reference internal" href="#rationale">Rationale</a></li>
<li><a class="reference internal" href="#specification">Specification</a></li>
<li><a class="reference internal" href="#naming-discussion">Naming Discussion</a></li>
<li><a class="reference internal" href="#references">References</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>Input and output are core features of computer programs. Currently,
Python provides a simple means of output through the print keyword
and two simple means of interactive input through the input()
and raw_input() built-in functions.</p>
<p>Python 3.0 will introduce various incompatible changes with previous
Python versions (<a class="pep reference internal" href="../pep-3100/" title="PEP 3100 Miscellaneous Python 3.0 Plans">PEP 3100</a>).
Among the proposed changes, print will become a built-in
function, print(), while input() and raw_input() would be removed completely
from the built-in namespace, requiring importing some module to provide
even the most basic input capability.</p>
<p>This PEP proposes that Python 3.0 retains some simple interactive user
input capability, equivalent to raw_input(), within the built-in namespace.</p>
<p>It was accepted by the BDFL in December 2006 <a class="footnote-reference brackets" href="#id8" id="id1">[5]</a>.</p>
</section>
<section id="motivation">
<h2><a class="toc-backref" href="#motivation" role="doc-backlink">Motivation</a></h2>
<p>With its easy readability and its support for many programming styles
(e.g. procedural, object-oriented, etc.) among others, Python is perhaps
the best computer language to use in introductory programming classes.
Simple programs often need to provide information to the user (output)
and to obtain information from the user (interactive input).
Any computer language intended to be used in an educational setting should
provide straightforward methods for both output and interactive input.</p>
<p>The <a class="pep reference internal" href="../pep-3100/" title="PEP 3100 Miscellaneous Python 3.0 Plans">current proposals for Python 3.0</a>
include a simple output pathway
via a built-in function named print(), but a more complicated method for
input [e.g. via sys.stdin.readline()], one that requires importing an external
module. Current versions of Python (pre-3.0) include raw_input() as a
built-in function. With the availability of such a function, programs that
require simple input/output can be written from day one, without requiring
discussions of importing modules, streams, etc.</p>
</section>
<section id="rationale">
<h2><a class="toc-backref" href="#rationale" role="doc-backlink">Rationale</a></h2>
<p>Current built-in functions, like input() and raw_input(), are found to be
extremely useful in traditional teaching settings. (For more details,
see <a class="footnote-reference brackets" href="#id5" id="id2">[2]</a> and the discussion that followed.)
While the BDFL has clearly stated <a class="footnote-reference brackets" href="#id6" id="id3">[3]</a> that input() was not to be kept in
Python 3000, he has also stated that he was not against revising the
decision of killing raw_input().</p>
<p>raw_input() provides a simple mean to ask a question and obtain a response
from a user. The proposed plans for Python 3.0 would require the replacement
of the single statement:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">name</span> <span class="o">=</span> <span class="n">raw_input</span><span class="p">(</span><span class="s2">&quot;What is your name?&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>by the more complicated:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;What is your name?&quot;</span><span class="p">)</span>
<span class="n">same</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
</pre></div>
</div>
<p>However, from the point of view of many Python beginners and educators, the
use of sys.stdin.readline() presents the following problems:</p>
<p>1. Compared to the name “raw_input”, the name “sys.stdin.readline()”
is clunky and inelegant.</p>
<p>2. The names “sys” and “stdin” have no meaning for most beginners,
who are mainly interested in <em>what</em> the function does, and not <em>where</em>
in the package structure it is located. The lack of meaning also makes
it difficult to remember:
is it “sys.stdin.readline()”, or “ stdin.sys.readline()”?
To a programming novice, there is not any obvious reason to prefer
one over the other. In contrast, functions simple and direct names like
print, input, and raw_input, and open are easier to remember.</p>
<p>3. The use of “.” notation is unmotivated and confusing to many beginners.
For example, it may lead some beginners to think “.” is a standard
character that could be used in any identifier.</p>
<p>4. There is an asymmetry with the print function: why is print not called
sys.stdout.print()?</p>
</section>
<section id="specification">
<h2><a class="toc-backref" href="#specification" role="doc-backlink">Specification</a></h2>
<p>The existing <code class="docutils literal notranslate"><span class="pre">raw_input()</span></code> function will be renamed to <code class="docutils literal notranslate"><span class="pre">input()</span></code>.</p>
<p>The Python 2 to 3 conversion tool will replace calls to <code class="docutils literal notranslate"><span class="pre">input()</span></code> with
<code class="docutils literal notranslate"><span class="pre">eval(input())</span></code> and <code class="docutils literal notranslate"><span class="pre">raw_input()</span></code> with <code class="docutils literal notranslate"><span class="pre">input()</span></code>.</p>
</section>
<section id="naming-discussion">
<h2><a class="toc-backref" href="#naming-discussion" role="doc-backlink">Naming Discussion</a></h2>
<p>With <code class="docutils literal notranslate"><span class="pre">input()</span></code> effectively removed from the language,
the name <code class="docutils literal notranslate"><span class="pre">raw_input()</span></code> makes much less sense and alternatives should be
considered. The various possibilities mentioned in various forums include:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ask</span><span class="p">()</span>
<span class="n">ask_user</span><span class="p">()</span>
<span class="n">get_string</span><span class="p">()</span>
<span class="nb">input</span><span class="p">()</span> <span class="c1"># initially rejected by BDFL, later accepted</span>
<span class="n">prompt</span><span class="p">()</span>
<span class="n">read</span><span class="p">()</span>
<span class="n">user_input</span><span class="p">()</span>
<span class="n">get_response</span><span class="p">()</span>
</pre></div>
</div>
<p>While it was initially rejected by the BDFL, it has been suggested that the
most direct solution would be to rename “raw_input” to “input” in Python 3000.
The main objection is that Python 2.x already has a function named “input”,
and, even though it is not going to be included in Python 3000,
having a built-in function with the same name but different semantics may
confuse programmers migrating from 2.x to 3000. Certainly, this is no problem
for beginners, and the scope of the problem is unclear for more experienced
programmers, since raw_input(), while popular with many, is not in
universal use. In this instance, the good it does for beginners could be
seen to outweigh the harm it does to experienced programmers -
although it could cause confusion for people reading older books or tutorials.</p>
<p>The rationale for accepting the renaming can be found here <a class="footnote-reference brackets" href="#id7" id="id4">[4]</a>.</p>
</section>
<section id="references">
<h2><a class="toc-backref" href="#references" role="doc-backlink">References</a></h2>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="id5" role="doc-footnote">
<dt class="label" id="id5">[<a href="#id2">2</a>]</dt>
<dd>The fate of raw_input() in Python 3000
<a class="reference external" href="https://mail.python.org/pipermail/edu-sig/2006-September/006967.html">https://mail.python.org/pipermail/edu-sig/2006-September/006967.html</a></aside>
<aside class="footnote brackets" id="id6" role="doc-footnote">
<dt class="label" id="id6">[<a href="#id3">3</a>]</dt>
<dd>Educational aspects of Python 3000
<a class="reference external" href="https://mail.python.org/pipermail/python-3000/2006-September/003589.html">https://mail.python.org/pipermail/python-3000/2006-September/003589.html</a></aside>
<aside class="footnote brackets" id="id7" role="doc-footnote">
<dt class="label" id="id7">[<a href="#id4">4</a>]</dt>
<dd>Rationale for going with the straight renaming
<a class="reference external" href="https://mail.python.org/pipermail/python-3000/2006-December/005249.html">https://mail.python.org/pipermail/python-3000/2006-December/005249.html</a></aside>
<aside class="footnote brackets" id="id8" role="doc-footnote">
<dt class="label" id="id8">[<a href="#id1">5</a>]</dt>
<dd>BDFL acceptance of the PEP
<a class="reference external" href="https://mail.python.org/pipermail/python-3000/2006-December/005257.html">https://mail.python.org/pipermail/python-3000/2006-December/005257.html</a></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-3111.rst">https://github.com/python/peps/blob/main/peps/pep-3111.rst</a></p>
<p>Last modified: <a class="reference external" href="https://github.com/python/peps/commits/main/peps/pep-3111.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="#motivation">Motivation</a></li>
<li><a class="reference internal" href="#rationale">Rationale</a></li>
<li><a class="reference internal" href="#specification">Specification</a></li>
<li><a class="reference internal" href="#naming-discussion">Naming Discussion</a></li>
<li><a class="reference internal" href="#references">References</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-3111.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>