在Python 2.x中,在长整数后面有一个L
后缀。由于Python 3将所有整数视为长整数,因此已将其删除。来自What's New In Python 3.0:
长整数的repr()不再包含尾随L,因此无条件地剥离该字符的代码将切掉最后一位数字。 (改用str()。)
从此我得到repr()
不会显示L
后缀,但str()
会有L
后缀。但是在Python 3.3.3中,没有一个显示L
后缀。
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()
的输出不应该是18446744073709551616L
吗?我在What's New In Python 3.1,What's New In Python 3.2和What's New In Python 3.3中找不到L
后缀从str()
中删除的内容。 3.2说:
浮点数或复数的str()现在与其repr()相同。
但它没有提到整数。
在L
中还删除了哪个版本的Python str()
后缀?或者我错过了一些明显的东西?
答案 0 :(得分:11)
你误解了文档。
该评论针对的是试图从Python 2中的L
中删除repr()
的人。这些人可以使用str()
代替并获得相同的号码,而无需每次都删除L
。
换句话说,str()
在Python 2中的长整数上使用时,是将数字转换为字符串的更好方法,因为它永远不会添加L
后缀repr()
1}}会添加:
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()
时。没有意义; <3> Python 3中的所有整数都是长整数。