简体   繁体   English

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

[英]L suffix in long integer in Python 3.x

In Python 2.x there was a L suffix after long integer. 在Python 2.x中,在长整数后面有一个L后缀。 As Python 3 treats all integers as long integer this has been removed. 由于Python 3将所有整数视为长整数,因此已将其删除。 From What's New In Python 3.0 : Python 3.0中的新功能

The repr() of a long integer doesn't include the trailing L anymore, so code that unconditionally strips that character will chop off the last digit instead. 长整数的repr()不再包含尾随L,因此无条件地剥离该字符的代码将切掉最后一位数字。 (Use str() instead.) (改用str()。)

From this I get that repr() won't show L suffix, but str() will have the L suffix. 从这里我得到repr()不会显示L后缀,但str()将具有L后缀。 But in Python 3.3.3 none of them are showing L suffix. 但在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'

Shouldn't the output of str() be 18446744073709551616L as per the doc? 根据文档, str()的输出不应该是18446744073709551616L吗? I could not find anything in What's New In Python 3.1 , What's New In Python 3.2 and What's New In Python 3.3 that says L suffix is removed from str() too. 在Python 3.1中 的新功能,Python 3.2中 的新功能以及Python 3.3的新功能中找不到任何内容,表示L后缀也从str()删除。 3.2 says that: 3.2说:

The str() of a float or complex number is now the same as its repr(). 浮点数或复数的str()现在与其repr()相同。

But it says nothing about integer. 但它没有说整数。

From which version of Python L suffix is removed in str() too? str()删除了哪个版本的Python L后缀? Or am I missing something obvious? 还是我错过了一些明显的东西?

You misunderstood the documentation. 你误解了文档。

The remark is aimed at people trying to strip the L from repr() in Python 2 . 这句话的目的是试图从Python 2中的 repr() 删除L Those people could use str() instead and get the same number without having to strip the L each time. 那些人可以使用str()来获得相同的数字,而不必每次都去掉L

In other words, str() , when used on a long integer in Python 2, is the better method to convert the number to a string, as it will never add the L suffix that repr() would add: 换句话说, str()在Python 2中的长整数上使用时,是将数字转换为字符串的更好方法,因为它永远不会添加repr()将添加的L后缀:

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 will never add the L . Python 3永远不会添加L Not when using repr() , and not when using str() . 不是在使用repr() ,而不是在使用str() There would be no point; 没有意义; all integers in Python 3 are long integers. Python 3中的所有整数都是长整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM