• <small id='8rOo7'></small><noframes id='8rOo7'>

  • <tfoot id='8rOo7'></tfoot>
      <bdo id='8rOo7'></bdo><ul id='8rOo7'></ul>
    <i id='8rOo7'><tr id='8rOo7'><dt id='8rOo7'><q id='8rOo7'><span id='8rOo7'><b id='8rOo7'><form id='8rOo7'><ins id='8rOo7'></ins><ul id='8rOo7'></ul><sub id='8rOo7'></sub></form><legend id='8rOo7'></legend><bdo id='8rOo7'><pre id='8rOo7'><center id='8rOo7'></center></pre></bdo></b><th id='8rOo7'></th></span></q></dt></tr></i><div id='8rOo7'><tfoot id='8rOo7'></tfoot><dl id='8rOo7'><fieldset id='8rOo7'></fieldset></dl></div>
      <legend id='8rOo7'><style id='8rOo7'><dir id='8rOo7'><q id='8rOo7'></q></dir></style></legend>

        为什么 Python 3 允许“00"?作为 0 的文字,但不允许使用“01";作为 1 的文字?

        Why does Python 3 allow quot;00quot; as a literal for 0 but not allow quot;01quot; as a literal for 1?(为什么 Python 3 允许“00?作为 0 的文字,但不允许使用“01;作为 1 的文字?)
            <tbody id='pTCjN'></tbody>
              <bdo id='pTCjN'></bdo><ul id='pTCjN'></ul>

                  <i id='pTCjN'><tr id='pTCjN'><dt id='pTCjN'><q id='pTCjN'><span id='pTCjN'><b id='pTCjN'><form id='pTCjN'><ins id='pTCjN'></ins><ul id='pTCjN'></ul><sub id='pTCjN'></sub></form><legend id='pTCjN'></legend><bdo id='pTCjN'><pre id='pTCjN'><center id='pTCjN'></center></pre></bdo></b><th id='pTCjN'></th></span></q></dt></tr></i><div id='pTCjN'><tfoot id='pTCjN'></tfoot><dl id='pTCjN'><fieldset id='pTCjN'></fieldset></dl></div>

                  <small id='pTCjN'></small><noframes id='pTCjN'>

                  <tfoot id='pTCjN'></tfoot>
                • <legend id='pTCjN'><style id='pTCjN'><dir id='pTCjN'><q id='pTCjN'></q></dir></style></legend>
                  本文介绍了为什么 Python 3 允许“00"?作为 0 的文字,但不允许使用“01";作为 1 的文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  为什么 Python 3 允许00"作为 0 的文字,但不允许01"作为 1 的文字?有充分的理由吗?这种不一致让我感到困惑.(我们谈论的是 Python 3,它故意破坏向后兼容性以实现一致性等目标.)

                  Why does Python 3 allow "00" as a literal for 0 but not allow "01" as a literal for 1? Is there a good reason? This inconsistency baffles me. (And we're talking about Python 3, which purposely broke backward compatibility in order to achieve goals like consistency.)

                  例如:

                  >>> from datetime import time
                  >>> time(16, 00)
                  datetime.time(16, 0)
                  >>> time(16, 01)
                    File "<stdin>", line 1
                      time(16, 01)
                                ^
                  SyntaxError: invalid token
                  >>>
                  

                  推荐答案

                  根据 https://docs.python.org/3/reference/lexical_analysis.html#integer-literals:

                  整数字面量由以下词法定义描述:

                  Integer literals are described by the following lexical definitions:

                  integer        ::=  decimalinteger | octinteger | hexinteger | bininteger
                  decimalinteger ::=  nonzerodigit digit* | "0"+
                  nonzerodigit   ::=  "1"..."9"
                  digit          ::=  "0"..."9"
                  octinteger     ::=  "0" ("o" | "O") octdigit+
                  hexinteger     ::=  "0" ("x" | "X") hexdigit+
                  bininteger     ::=  "0" ("b" | "B") bindigit+
                  octdigit       ::=  "0"..."7"
                  hexdigit       ::=  digit | "a"..."f" | "A"..."F"
                  bindigit       ::=  "0" | "1"
                  

                  整数文字的长度没有限制,除了可以存储在可用内存中.

                  There is no limit for the length of integer literals apart from what can be stored in available memory.

                  请注意,不允许在非零十进制数中使用前导零.这是为了消除 C 风格的八进制文字的歧义,Python在 3.0 之前使用.

                  Note that leading zeros in a non-zero decimal number are not allowed. This is for disambiguation with C-style octal literals, which Python used before version 3.0.

                  如此处所述,非零十进制数中的前导零是不允许的."0"+ 作为一个非常特殊的情况是合法的,它 在 Python 2 中不存在:

                  As noted here, leading zeros in a non-zero decimal number are not allowed. "0"+ is legal as a very special case, which wasn't present in Python 2:

                  integer        ::=  decimalinteger | octinteger | hexinteger | bininteger
                  decimalinteger ::=  nonzerodigit digit* | "0"
                  octinteger     ::=  "0" ("o" | "O") octdigit+ | "0" octdigit+
                  

                  <小时>

                  SVN commit r55866 实现了 PEP3127 在标记器中,它禁止旧的 0 数字.然而,奇怪的是,它还添加了这个注释:


                  SVN commit r55866 implemented PEP 3127 in the tokenizer, which forbids the old 0<octal> numbers. However, curiously, it also adds this note:

                  /* in any case, allow '0' as a literal */
                  

                  带有一个特殊的 nonzero 标志,仅当以下数字序列包含非零数字时才抛出 SyntaxError.

                  with a special nonzero flag that only throws a SyntaxError if the following sequence of digits contains a nonzero digit.

                  这很奇怪,因为 PEP 3127 不允许这种情况:

                  This is odd because PEP 3127 does not allow this case:

                  此 PEP 提议将在 Python 3.0(以及 2.6 的 Python 3.0 预览模式)中删除使用前导零指定八进制数的功能,并且将在任何时候引发 SyntaxError前导0"后紧跟另一个数字.

                  This PEP proposes that the ability to specify an octal number by using a leading zero will be removed from the language in Python 3.0 (and the Python 3.0 preview mode of 2.6), and that a SyntaxError will be raised whenever a leading "0" is immediately followed by another digit.

                  (强调我的)

                  因此,允许多个零的事实在技术上违反了 PEP,并且基本上是由 Georg Brandl 作为特例实现的.他对文档进行了相应的更改,指出 "0"+decimalinteger 的有效案例(之前已在 octinteger 中介绍过).

                  So, the fact that multiple zeros are allowed is technically violating the PEP, and was basically implemented as a special case by Georg Brandl. He made the corresponding documentation change to note that "0"+ was a valid case for decimalinteger (previously that had been covered under octinteger).

                  我们可能永远不会确切地知道为什么 Georg 选择使 "0"+ 有效 - 它可能永远是 Python 中一个奇怪的极端情况.

                  We'll probably never know exactly why Georg chose to make "0"+ valid - it may forever remain an odd corner case in Python.

                  更新 [2015 年 7 月 28 日]:这个问题导致了 关于 python-ideas 的热烈讨论帖 其中 Georg 插话:

                  UPDATE [28 Jul 2015]: This question led to a lively discussion thread on python-ideas in which Georg chimed in:

                  史蒂文·达普拉诺写道:

                  Steven D'Aprano wrote:

                  为什么要这样定义?[...] 为什么我们要写 0000 来得到零?

                  Why was it defined that way? [...] Why would we write 0000 to get zero?

                  我可以告诉你,但我必须杀了你.

                  I could tell you, but then I'd have to kill you.

                  乔治

                  后来,该线程产生了此错误报告,旨在摆脱这种特殊情况.在这里,Georg 说:

                  Later on, the thread spawned this bug report aiming to get rid of this special case. Here, Georg says:

                  我不记得这种故意更改的原因(从文档更改中可以看出).

                  I don't recall the reason for this deliberate change (as seen from the docs change).

                  我现在无法为这种更改找到充分的理由 [...]

                  I'm unable to come up with a good reason for this change now [...]

                  因此我们得到了它:这种不一致背后的确切原因已被时间遗忘.

                  and thus we have it: the precise reason behind this inconsistency is lost to time.

                  最后,请注意错误报告已被拒绝:对于 Python 3.x 的其余部分,将继续仅接受零整数上的前导零.

                  Finally, note that the bug report was rejected: leading zeros will continue to be accepted only on zero integers for the rest of Python 3.x.

                  这篇关于为什么 Python 3 允许“00"?作为 0 的文字,但不允许使用“01";作为 1 的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                      <tbody id='hBstA'></tbody>
                      <i id='hBstA'><tr id='hBstA'><dt id='hBstA'><q id='hBstA'><span id='hBstA'><b id='hBstA'><form id='hBstA'><ins id='hBstA'></ins><ul id='hBstA'></ul><sub id='hBstA'></sub></form><legend id='hBstA'></legend><bdo id='hBstA'><pre id='hBstA'><center id='hBstA'></center></pre></bdo></b><th id='hBstA'></th></span></q></dt></tr></i><div id='hBstA'><tfoot id='hBstA'></tfoot><dl id='hBstA'><fieldset id='hBstA'></fieldset></dl></div>

                        <small id='hBstA'></small><noframes id='hBstA'>

                        <tfoot id='hBstA'></tfoot>

                        <legend id='hBstA'><style id='hBstA'><dir id='hBstA'><q id='hBstA'></q></dir></style></legend>
                          <bdo id='hBstA'></bdo><ul id='hBstA'></ul>