Python 3.x 中长整型中的 L 后缀

2024-04-21

在Python 2.x中有一个L长整型后的后缀。由于 Python 3 将所有整数视为长整数,因此这一点已被删除。从Python 3.0 的新增功能 https://docs.python.org/3.0/whatsnew/3.0.html#integers:

长整数的 repr() 不再包含尾随的 L,因此无条件删除该字符的代码将截断最后一个数字。 (请改用 str()。)

从这里我明白了repr()不会显示L后缀,但是str()将有L后缀。但在 Python 3.3.3 中没有一个显示L suffix.

Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:19:30) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> repr(2 ** 64)
'18446744073709551616'
>>> str(2 ** 64)
'18446744073709551616'

不应该输出str() be 18446744073709551616L根据医生的说法?我找不到任何东西Python 3.1 的新增功能 https://docs.python.org/3/whatsnew/3.1.html, Python 3.2 的新增功能 https://docs.python.org/3/whatsnew/3.2.html and Python 3.3 的新增功能 https://docs.python.org/3/whatsnew/3.3.html说的是L后缀被删除str()也。 3.2 说:

浮点数或复数的 str() 现在与其 repr() 相同。

但它没有提到整数。

来自哪个版本的PythonL后缀被删除str()也?或者我错过了一些明显的东西?


您误解了文档。

这句话是针对那些试图剥夺L from repr() 在Python 2中。那些人可以使用str()相反,无需剥离即可获得相同的数字L每一次。

换句话说,str()当在 Python 2 中用于长整数时,是将数字转换为字符串的更好方法,因为它永远不会添加L后缀那个repr()会添加:

Python 2.7.6 (default, Apr 28 2014, 17:17:35) 
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print repr(1L)
1L
>>> print str(1L)
1

Python 3 永远不会添加L。使用时不repr(),而不是在使用时str()。这样就没有意义了;allPython 3 中的整数是长整数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 3.x 中长整型中的 L 后缀 的相关文章

随机推荐